Skip to content

'Any' with a bound? #8981

@mthuurne

Description

@mthuurne

When mypy is run on the following code:

from typing import Any, Generic, TypeVar

T = TypeVar('T', bound=int)

class C(Generic[T]):
    x: T

c: C[Any] = C()
c.x = 123
c.x = 'abc'

It will not report any errors. However, since T has int as a bound, assigning a string to x should be considered invalid even if we don't know the exact type of x.

It seems that when Any is substituted for the type argument of C, the knowledge of the bound is lost. Perhaps AnyType could be given a bound, object by default, to restrict which types Any can represent in a particular context.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions