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

Add type hints to constructors of field type classes #488

Merged
merged 1 commit into from
Jun 16, 2017

Conversation

KonishchevDmitry
Copy link

@KonishchevDmitry KonishchevDmitry commented May 16, 2017

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

Code I've tested it on:

from typing import List, Dict, Union, Tuple

import datetime
from decimal import Decimal

from schematics.models import Model
from schematics.types import StringType, ListType, ModelType, NumberType, IntType, FloatType, DecimalType, \
    BooleanType, DateType, DateTimeType, DictType

class TestNestedModel(Model):
    nested_string = StringType()

class TestModel(Model):
    string = StringType()
    boolean = BooleanType()
    integer = IntType()
    float = FloatType()
    number = NumberType()
    decimal = DecimalType()
    date = DateType()
    time = DateTimeType()
    dict = DictType(TestNestedModel)
    string_list = ListType(StringType())
    model = ModelType(TestNestedModel)
    model_list = ListType(ModelType(TestNestedModel))

class Bad: pass
invalid_type = Tuple[Bad, Bad, Bad, Bad, Bad, Bad, Bad, Bad, Bad, Bad, Bad, Bad, Bad]
valid_type = Tuple[str, bool, int, float, Union[int, float], Decimal, datetime.date, datetime.datetime,
                   Dict[str, TestNestedModel], List[str], TestNestedModel, List[TestNestedModel], str]

def test(valid: valid_type, invalid: invalid_type):
    pass

model = TestModel()
test_data = (
    model.string, model.boolean, model.integer, model.float, model.number, model.decimal, model.date,
    model.time, model.dict, model.string_list, model.model, model.model_list, model.model_list[0].nested_string
)
test(test_data, test_data)

@lkraider
Copy link
Contributor

That's really cool. I was playing with the typing module myself for the internals, I think it merits to be included.

@lkraider lkraider self-assigned this May 16, 2017
@lkraider lkraider closed this May 23, 2017
@lkraider lkraider reopened this May 23, 2017
@lkraider lkraider changed the base branch from development to master May 31, 2017 02:00
@lkraider
Copy link
Contributor

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]
Copy link
Contributor

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?

Copy link
Author

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.
@KonishchevDmitry
Copy link
Author

OK, removed. I added these comments just to prevent some contributors who don't know about type hinting from accidental break of my changes.

@lkraider lkraider merged commit 6557d7f into schematics:master Jun 16, 2017
@lkraider
Copy link
Contributor

Thanks for the cool patch!

@KonishchevDmitry
Copy link
Author

Thank you!

@KonishchevDmitry KonishchevDmitry deleted the pr-type-hints branch June 16, 2017 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants