-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Support type hints in model fields #1559
Comments
I think type hints are misguided and unpythonic, and it's my stance that they will never be supported by peewee. Python is a high-level dynamic language, peewee plays to these strengths. You couldn't implement peewee in go, it'd look completely different. I enjoy statically typed languages. When I want static typing I use c or go. When I want dynamic or metaprogramming I use python. These are my opinions and I understand that others have their own views. My goals for peewee are to make it composable and consistent, and I feel I can do that best by using python's dynamism. |
I understand your clause fully, just note that type hints could be supported where dynamic python magic is not needed. Plus in this case the library itself wouldn't imbue type hints - it would just support users declaring them. That would require changes (ie. something like what I suggested above) and I understand that you're reluctant to add that. Anyway, if you (or anyone here) comes up with a simpler idea, chime in. Maybe I'll ask about this on mypy tracker as well. I'll ping back here if there's any further insight. |
@tuukkamustonen oh I've just got your proposition. I'ts cludgy. I come to something like User.c.name (stealed from sqlalchemy). My temporary solution is to turn off typing in queries or cast User.name to any (sadly python have no typescript-style casts like (obj as any) for that purpose, but we have |
@tuukkamustonen, @enomado can you share a snippet of code of how you "by pass" mypy errors.
|
Well I could silence
and tested with:
At the end it seems with can add a simple peewee |
Static type check is great tool and like unit test and code coverage you don't have to check it all. Some kind of developers, like me, need all the help from tools that we can have. To me type annotations is more useful than normal doc comments. It forces me to have them updated and follow type hints PEP 484 specs. Python dynamism is great for create flexible libs, frameworks and tools like |
@bySabi I think your problem is different than what we discussed here? You had:
That probably just tells that your (virtual)environment/PYTHONPATH is somehow messed up, because it's not finding I don't think this issue can be handled through type stubs ( |
@tuukkamustonen it's possible that I missed something on I think the problem with objects: SqliteDatabase, Model, IntergerField ... it's because they were imported using In the end I could solve it using above And your are right, my typing hint problem don't have nothing to do with this issue. But anyway annotate models with types IMO is lot of duplicate work cause peewee type Fields (IntergerField, CharField, ...) will do the typecheck/validation at runtime. |
I would also like to request this, because it helps with code completion. For example, consider the code:
This returns a <Model: User>, but it would be great if it type hinted to a User object, because on the next line of code, I would get a list of functions available to user (for example, if the User class had a function Also, is it possible to specify a return collection type? For example, in SQLAlchemy, I can do something like
and the orders will be returned as an Orders object, which is a custom class object that inherits from InstrumentedList. This allows me to add special functions, such as .most_recent(), .to_df(), etc... |
Being aware that you don't like type hints (#1298), I'll be bold and ask if you would allow users to annotate their models with type hints?
Problem:
Cannot:
Would it be possible to have separate fields for class and instance:
The idea being that instance would override the
name
field with getter/setter (declared asinstance_field
).Yeah it is hackish, but it would be better than having to
# type: <override>
everywhere.I believe this couldn't go to typeshed stubs anyway due to being context-specific.
The text was updated successfully, but these errors were encountered: