groc-prog / pyneo4j-ogm

A asynchronous Object-Graph-Mapper for Neo4j 5+ and Python 3.10+
MIT License
13 stars 2 forks source link

Call expression not allowed in type expression #4

Open siwoo-destim opened 15 hours ago

siwoo-destim commented 15 hours ago

Call expression not allowed in type expression

I really like this library due to its Pydantic-friendly features. I hope more people get to use this repository.

However, when I use "WithOptions" while defining a model, I encounter some issues:

def test():
    return "test"

class User(NodeModel):
    profile_image: str
    bio: str
    display_name: str

    username: WithOptions(str, unique=True) = Field(default_factory=test)

image

The type checker gives me a warning, and it's not ideal to see that. After some searching, I found a related issue on this page:

https://github.com/microsoft/pylance-release/issues/5457 (According to above issues post, it seems to be caused by Pylance update) So, I tried the following code:

def test():
    return "test"

class User(NodeModel):
    profile_image: str
    bio: str
    display_name: str
    username: Annotated[str, WithOptions(str, unique=True)] = Field(default_factory=test)

While warning has disappeared, "WithOptions" doesn't guarantee uniqueness.

Your environment

Steps to reproduce

def test():
    return "test"

class User(NodeModel):
    profile_image: str
    bio: str
    display_name: str

    username: WithOptions(str, unique=True) = Field(default_factory=test)

Call expression not allowed in type expression

Expected Behavior

No error

Actual Behavior

Call expression not allowed in type expression

groc-prog commented 10 hours ago

Hey, I'm happy you like the library so far! I did not have any time to work on anything lately due to personal reasons, but I am planning to get started again some time soon (fingers crossed).

Regarding your issue, it's something we used to just supress as mentioned in here in our production codebases. I currently don't know of any possible workaround, as I think we would need to implement it some other way for type checkers to not complain.

If you have any suggestions for another implementation, feel free to share your thoughts. Breaking changes wouldn't be a problem, since I plan on reworking some core mechanics for the next release anyways, once i find the time for it.