Skip to content
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

Reject bound covariant type variable in List argument #9269

Closed
wants to merge 1 commit into from

Conversation

LiuYuHui
Copy link
Contributor

@LiuYuHui LiuYuHui commented Aug 5, 2020

Fixes #734

@@ -947,6 +947,14 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str])
if ctx.line < 0:
ctx = typ
self.fail(message_registry.FUNCTION_PARAMETER_CANNOT_BE_COVARIANT, ctx)
elif is_named_instance(arg_type, 'builtins.list'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about Dict and (possibly) other types?

from typing import Dict, TypeVar

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

def cov(a: Dict[T_co, T_co]) -> Dict[T_co, T_co]:
    ...

def contra(a: Dict[T_contra, T_contra]) -> Dict[T_contra, T_contra]:
    ...

reveal_type(cov({'a': 1}))
reveal_type(contra({'a': 1}))

Outputs:

ex.py:12: error: Cannot infer type argument 1 of "cov"
ex.py:12: note: Revealed type is 'builtins.dict[Any, Any]'
ex.py:13: error: Cannot infer type argument 1 of "contra"
ex.py:13: note: Revealed type is 'builtins.dict[Any, Any]'

@msullivan
Copy link
Collaborator

I don't think we want to take a PR that does this only for List, and not other things. Happy to discuss how to generalize it.

(I'm closing the PR as part of trying to tidy up, but I'm happy to work with you on moving forward if you're still interested)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reject bound covariant type variable in an argument type
3 participants