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

Improved type hinting for models #462

Open
robinvandernoord opened this issue Jun 20, 2023 · 1 comment
Open

Improved type hinting for models #462

robinvandernoord opened this issue Jun 20, 2023 · 1 comment

Comments

@robinvandernoord
Copy link

Since according to the documentation field types are hinted to specific Python objects, it would be great to be able to type hint the result of queries as a specific model with the right types for each property.

user_1: User = db(db.User.id == 1).select().first()

# user_2 could automatically be hinted to User
user_2 = db.User(id=2)

group_1.name # currently a Field unless I define `name: str = Field()`

Of course, assigning to Field() can't figure out the right type, but maybe doing name = Field.string() can provide this behavior?
Possibly, this could interfere with Query creation but it would be a really nice feature to have IntelliSense for database rows.
It's just a suggestion, for now I will just use my own subclass of Field:

class TypedField(Field):
    @classmethod
    def string(cls, *args, **kwargs) -> str:
        return super().string(*args, **kwargs)

    @classmethod
    def int(cls, *args, **kwargs) -> int:
        return super().int(*args, **kwargs)

# etc...
@gi0baro
Copy link
Member

gi0baro commented Jun 20, 2023

This is actually tracked in emmett-framework/firestorm#5
Some ORM issues were moved there because of #309

Specifically regarding your suggestions, actually:

User.get(1)

does not return a User object. It returns a dynamic class UserRow generated by the User model which is actually a subclass of StructuredRow.

So while binding types to fields would be easy, managing type hints on several ORM functions will be more complicated.

Btw I just saw you made https://github.com/trialandsuccess/TypeDAL, so even if I didn't check the code, I think it could be a starting point also for the Emmett ORM.

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

No branches or pull requests

2 participants