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

Give useful message if can't infer TypeVar-based type for attribute #3651

Merged
merged 2 commits into from Jul 18, 2017

Conversation

Projects
None yet
2 participants
@JukkaL
Collaborator

JukkaL commented Jul 4, 2017

Require a type annotation if the inferred attribute type includes a
type variable with values.

Fix #2697 by giving a better error message. Fixing type inference
would be much more complicated. This partial fix should be fine as the
issue seems to arise pretty rarely.

Mypy processes methods of a generic class that has a type variable
with values by type checking the methods multiple times, once for each
type variable value. This implementation detects if different type
checking passes infer a different type for an attribute.

The extra data structure for storing inferred attibute types helps
with with fine-grained incremental checking.

JukkaL added some commits Jul 3, 2017

Give useful message if can't infer TypeVar-based type for attribute
Require a type annotation if the inferred attribute type includes a
type variable with values.

Fix #2697 by giving a better error message. Fixing type inference
would be much more complicated. This partial fix should be fine as the
issue seems to arise pretty rarely.

Mypy processes methods of a generic class that has a type variable
with values by type checking the methods multiple times, once for each
type variable value. This implementation detects if different type
checking passes infer a different type for an attribute.

The extra data structure for storing inferred attibute types helps
with with fine-grained incremental checking.
@ilevkivskyi

This quick fix looks good to me. Finally, maybe a user wants to have:

T = TypeVar('T', str, int)
...
    def __init__(self, x: T) -> None:
        self.x: Union[int, str] = x

:-)

@ilevkivskyi ilevkivskyi merged commit c2a262d into master Jul 18, 2017

4 checks passed

continuous-integration/appveyor/branch AppVeyor build succeeded
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

@ilevkivskyi ilevkivskyi deleted the infer-typevar-values branch Jul 18, 2017

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