Closed
Description
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.
from abc import abstractmethod, ABCMeta
from typing import Any, TypeVar
class Comparable(metaclass=ABCMeta):
@abstractmethod
def __lt__(self, other: Any) -> bool: ...
... # __gt__ etc. as well
CT = TypeVar('CT', bound=Comparable)
def min(x: CT, y: CT) -> CT:
if x < y:
return x
else:
return y
min(1, 2) # ok, return type int
min('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:
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.770
hooks:
- id: mypy
args: [--disallow-untyped-defs, --show-error-codes]
I am on Python 3.7.3.
Metadata
Metadata
Assignees
Labels
No labels