KEMBAR78
Give useful message if can't infer TypeVar-based type for attribute by JukkaL · Pull Request #3651 · python/mypy · GitHub
Skip to content

Conversation

JukkaL
Copy link
Collaborator

@JukkaL 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 2 commits July 4, 2017 10:13
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.
Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@ilevkivskyi ilevkivskyi deleted the infer-typevar-values branch July 18, 2017 08:21
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.

Cannot determine type error with TypeVar with value constraint

2 participants