Skip to content

'Any' with a bound? #8981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mthuurne opened this issue Jun 9, 2020 · 4 comments
Closed

'Any' with a bound? #8981

mthuurne opened this issue Jun 9, 2020 · 4 comments

Comments

@mthuurne
Copy link
Contributor

mthuurne commented Jun 9, 2020

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.

@Kangaroux
Copy link

Kangaroux commented Jun 10, 2020

https://docs.python.org/3/library/typing.html#typing.TypeVar

Alternatively, a type variable may specify an upper bound using bound=. This means that an actual type substituted (explicitly or implicitly) for the type variable must be a subclass of the boundary type [...]

I think it should report an error for c: C[Any] = C() rather than c.x = 'abc', since Any is a superset of int, not a subset.

@mthuurne
Copy link
Contributor Author

https://docs.python.org/3/library/typing.html#typing.TypeVar

Alternatively, a type variable may specify an upper bound using bound=. This means that an actual type substituted (explicitly or implicitly) for the type variable must be a subclass of the boundary type [...]

I think it should report an error for c: C[Any] = C() rather than c.x = 'abc', since Any is a superset of int, not a subset.

The typing module documentation you linked describes Any as:

Special type indicating an unconstrained type.

  • Every type is compatible with Any.
  • Any is compatible with every type.

Since it's compatible in both directions, I think C[Any] is valid.

@Akuli
Copy link
Contributor

Akuli commented Jul 25, 2020

Plain C is same as C[Any]. Should that be C[bound of T] instead?

@erictraut
Copy link

This issue appears to be based on a misunderstanding of how Any works in the Python type system. For details about how Any is meant to work and why it exists, refer to PEP 483.

The use of Any as a type argument is especially important in cases where the type parameter is invariant, as the use of any concrete type such as object or bound of T will result in invariant type incompatibilities.

Mypy is doing the right thing in how it's treating Any, and it would be inappropriate to emit an error in any of the above cases.

I recommend closing this issue.

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants