Support type hints in model fields
See original GitHub issueBeing 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:
class User(peewee.Model):
name = CharField()
u = User()
u.name = 'Joe' # mypy will nag because inferred type is 'CharField'
Cannot:
class User(peewee.Model):
name = CharField() # type: str
u = User()
u.name = 'Joe' # sure works now but
User.select().where(User.name.id == 1) # violation as User.name is marked as 'str'
Would it be possible to have separate fields for class and instance:
class User(peewee.Model):
name_c = CharField(instance_field='name')
name = None # type: str - simply a placeholder for mypy type
name: str # in py3.6+
u = User()
u.name = 'Joe' # would now have 'str' type
User.select().where(User.name_c.id == 1) # would have 'CharField' type
The idea being that instance would override the name
field with getter/setter (declared as instance_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.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Type hints cheat sheet - mypy 0.991 documentation
Type hints cheat sheet#. This document is a quick cheat sheet showing how to use type annotations for various common types in Python....
Read more >Adding type hints to the Django ORM - Will McGugan
The TypedModel class could inspect the type hints and create the integer field in the same way as models.Model uses IntegerField and friends....
Read more >typing — Support for type hints — Python 3.11.1 documentation
This module provides runtime support for type hints. The most fundamental support consists of the types Any , Union , Callable , TypeVar...
Read more >Type Hint for Django model with annotated field - Stack Overflow
Let's say I have the following Django models:
Read more >Django models Type Hinting without the cyclic import issue
Python introduced typing in version 3.5. It helps a lot in projects that are shared among large teams since it provides kind-of built-in ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub 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 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
user_function()
, I would see user_function() in the list of suggestions in the code completion hints. I know that I can specify this using # type: User, but I’d prefer it be automatic.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…