You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Protocols should be interpreted correctly when implicitly defined and using @Property decorator. Though if the @Property method is not used opting for the shorter syntax with a public access variable, no error is produced.
Actual behaviour
Strange error messages appear (with the red underline) stating things like: "int" is incompatible with "int"Pylance (reportGeneralTypeIssues) or "str" is incompatible with "str"Pylance (reportGeneralTypeIssues)
This produces the error: Argument of type "WithNameAndAge" cannot be assigned to parameter "v" of type "HasAgeProtocol" in function "print_age" "age" is an incompatible type "int" is incompatible with "int"Pylance (reportGeneralTypeIssues)
jaycosaur
changed the title
Pylance does not seem to work with implicit type definitions using Protocols
Pylance does not seem to work with implicit type definitions using Protocols and @property
Aug 10, 2020
This behavior is intended. The protocol you have defined specifies that the class must have a property called age, but your dataclass does not have any such property. A property is an object with different semantics than an instance variable, so they are not interchangeable. For example a property is not settable or deletable unless you provide a setter and a deleter.
If you change your protocol class to the following, it will work as you expect:
class HasAgeProtocol(Protocol):
age: int
Alternatively, if you define a class that actually defines a property age, it will work:
Environment data
Expected behaviour
Protocols should be interpreted correctly when implicitly defined and using @Property decorator. Though if the @Property method is not used opting for the shorter syntax with a public access variable, no error is produced.
Actual behaviour
Strange error messages appear (with the red underline) stating things like:
"int" is incompatible with "int"Pylance (reportGeneralTypeIssues)
or"str" is incompatible with "str"Pylance (reportGeneralTypeIssues)
Code Snippet / Additional information
This produces the error:
Argument of type "WithNameAndAge" cannot be assigned to parameter "v" of type "HasAgeProtocol" in function "print_age" "age" is an incompatible type "int" is incompatible with "int"Pylance (reportGeneralTypeIssues)
This works without error.
The text was updated successfully, but these errors were encountered: