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
Here is the code example in question. I added some missing imports at the top.
fromabcimportabstractmethod, ABCMetafromtypingimportAny, TypeVarclassComparable(metaclass=ABCMeta):
@abstractmethoddef__lt__(self, other: Any) ->bool: ...
... # __gt__ etc. as wellCT=TypeVar('CT', bound=Comparable)
defmin(x: CT, y: CT) ->CT:
ifx<y:
returnxelse:
returnymin(1, 2) # ok, return type intmin('x', 'y') # ok, return type str
mypy gives me this error:
test.py:17: error: Value of type variable "CT" of "min" cannot be "int" [type-var]
test.py:18: error: Value of type variable "CT" of "min" cannot be "str" [type-var]
Found 2 errors in 1 file (checked 1 source file)
Here is the pre-commit configuration I am using to run mypy:
Not sure about the history of that code in the PEP, but I believe Comparable has to be defined as a Protocol for it to work like that with mypy. For a working implementation, see: python/typing#59 (comment)
https://www.python.org/dev/peps/pep-0484/#type-variables-with-an-upper-bound
Here is the code example in question. I added some missing imports at the top.
mypy gives me this error:
Here is the pre-commit configuration I am using to run mypy:
I am on Python 3.7.3.
The text was updated successfully, but these errors were encountered: