-
Notifications
You must be signed in to change notification settings - Fork 287
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
Add type hints to constructors of field type classes #488
Conversation
That's really cool. I was playing with the typing module myself for the internals, I think it merits to be included. |
Hi, can you rebase and remove these comments: # ^ Override return type to suggest IDE the actual type of the object I'll then merge it in! |
@@ -83,7 +90,11 @@ def native_type(self): | |||
def fields(self): | |||
return self.model_class.fields | |||
|
|||
def __init__(self, model_spec, **kwargs): | |||
def __init__(self, | |||
model_spec, # type: typing.Type[T] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't this be represented in the doctype?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that it can't. I've tried various forms like # type: (typing.Type[T], ...) -> T
: it works for the model_spec
argument, but breaks other arguments (**kwargs
, and named parameters).
Type hints suggest IDE the actual type of the object. For example PyCharm can deduce actual types of fields of created Model objects - even for the nested ones.
e25beea
to
6ed9c17
Compare
OK, removed. I added these comments just to prevent some contributors who don't know about type hinting from accidental break of my changes. |
Thanks for the cool patch! |
Thank you! |
I've added type hints to constructors of field type classes to suggest IDE the actual type of the object and now PyCharm deduces types of all fields in model objects, even for the nested ones like
model.model_list[0].nested_string
which make auto-completion and refactoring to work much better.See the screenshot:
![Field type hinting in PyCharm](https://cloud.githubusercontent.com/assets/217795/26122548/9754c7f8-3a80-11e7-9e18-2b8672d913dd.png)
Code I've tested it on: