Skip to content

mypy gives error on PEP 484 bound= example #8889

Closed
@mcolen

Description

@mcolen

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions