Skip to content

Covariant type variables should be allowed in __init__ #2850

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
ilevkivskyi opened this issue Feb 11, 2017 · 2 comments
Closed

Covariant type variables should be allowed in __init__ #2850

ilevkivskyi opened this issue Feb 11, 2017 · 2 comments

Comments

@ilevkivskyi
Copy link
Member

There are several issues about covariance, but I didn't find any discussion about __init__. Currently, mypy complains about this code:

from typing import Generic, TypeVar

T_co = TypeVar('T_co', covariant=True)

class C(Generic[T_co]):
    def __init__(self, x: T_co) -> None:
       # Error: Cannot use a covariant type variable as a parameter
       ...

Although this is normal in general, I believe this should be allowed for __init__. Otherwise it would be difficult (if possible at all) to put something into a covariant (immutable) container on instantiation.

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 16, 2017

Yeah, this should be allowed. Not sure if there are some additional edge cases related to __init__ we should handle differently once we allow this -- x.__init__(...) is already rejected, which is good.

@ilevkivskyi
Copy link
Member Author

OK, here is a PR #2888 fixing this.

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

2 participants