Skip to content

Check kwargs key type as covariant #10223

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
wants to merge 1 commit into from
Closed

Check kwargs key type as covariant #10223

wants to merge 1 commit into from

Conversation

kamilturek
Copy link
Contributor

Closes #10023.

Assume mapping's key type is covariant when validating **kwargs argument type.

self.named_type('builtins.unicode')])
kwargs_type = self.chk.named_generic_type(
'typing.Mapping', [key_type, AnyType(TypeOfAny.special_form)])
kwargs_type.type.defn.type_vars[0].variance = COVARIANT
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't this affect all uses of Mapping after this code runs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, you're right. Didn't realized that before.

Choose a reason for hiding this comment

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

The fix I was thinking was something along these lines.

Suggested change
kwargs_type.type.defn.type_vars[0].variance = COVARIANT
is_subtype(typ, self.chk.named_type('typing.Mapping')) and isinstance(typ.args[0], LiteralType)

This wouldn't cover the case of Literal["a", "b"] but you can try using the try_getting_str_literals_from_type method

@kamilturek kamilturek closed this Mar 19, 2021
@kamilturek kamilturek deleted the string-literals-kwargs branch March 19, 2021 08:32
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.

Mypy incorrectly warns when passing literals as kwarg keywords
3 participants