Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttrOf isn't working for me #162

Open
jd-solanki opened this issue Oct 7, 2024 · 1 comment
Open

AttrOf isn't working for me #162

jd-solanki opened this issue Oct 7, 2024 · 1 comment

Comments

@jd-solanki
Copy link

jd-solanki commented Oct 7, 2024

Hi @sobolevn 👋🏻

I'm glad that I found this lib. I'm fan of TypeScript's and the type-fest lib.

I was really looking for something like type-fest in python ecosystem which allows addition type helpers until python gets more mature in types.

I use FastAPI and most of the time I want to create partial model of existing model.

from pydantic import BaseModel

class Person(BaseModel):
	name: str

class PersonPartial(BaseModel):
	name: str | None = None

For smaller model it's fine but for larger model it can lead to errors. So I was trying to use AttrOf like this:

from pydantic import BaseModel

class Person(BaseModel):
	name: str

class PersonPartial(BaseModel):
	name: AttrOf[Person, Literal["name"]] | None = None

PersonPartial() # Works
PersonPartial(name="wow") # Throws error for string

I don't know why second usage throws error 🤔

Also example given in the docs throws error for me as well
image


I also tried another way of using AttrOf which I really wanted but still it throws error:

class Test(BaseModel):
    name: str

def get_something(my_arg: AttrOf[Test, Literal["name"]]) -> None:
    pass

get_something("name") # Error

t = Test(name="wow")
get_something(t.name) # Error

image

Full Snippet
from typing import Literal, reveal_type

from mypy_extras import AttrOf
from openai import BaseModel


class User:
    def auth(self, username: str, password: str) -> bool:
        return False  # Just an example


def get_callback(user: User) -> AttrOf[User, Literal["auth"]]:
    return user.auth


user: User
reveal_type(get_callback(user))

reveal_type(AttrOf[User, Literal["wow"]])

# ---

class Person(BaseModel):
    name: str


class PersonPartial(BaseModel):
    name: AttrOf[Person, Literal["name"]] | None = None

PersonPartial() # Works
PersonPartial(name="wow") # Throws error for string

# ---

class Test(BaseModel):
    name: str

def get_something(my_arg: AttrOf[Test, Literal["name"]]) -> None:
    pass

get_something("name")

t = Test(name="wow")
get_something(t.name)

Can you help me with this? Also, I
would like to see more in mypy-extras. I'll try to understand how you are generating this utils like AttrOf and would love to contribute to python's type system to reach it to the level of typescript & type-fest.

Big Thanks ❤️

@sobolevn
Copy link
Member

sobolevn commented Oct 7, 2024

Glad that you liked it! This project is not very maintained one, because of how complex and fragile types are here.

If you are interested in contributing - I would be happy to review your PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants