Skip to content

Conversation

@randolf-scholz
Copy link
Contributor

@randolf-scholz randolf-scholz commented Dec 15, 2025

Fixes: #20424

Following my comments in #20416, this PR introduces a new helper function as_type that can be used to determine whether a type can be matched to a certain protocol or not.

As an example application, I fix a bug in is_valid_keyword_var_arg that stems from checking

kwargs <: SupportsKeyAndGetItem[str, Any]

This check is too eager, because SupportsKeyAndGetItem is invariant in the key type, it will produce a false positive when kwargs is for instance dict[Literal["foo", "bar"], int]. The correct test is:

Does there exist T <: str so that kwargs <: SupportsKeyAndGetItem[T, Any]

which can be checked with the new helper function.

Updated tests

I updated testLiteralKwargs to test:

  • both good and bad dict argument
  • both good and bad Mapping argument
  • both good and bad SupportsKeyAndGetitem argument

@github-actions

This comment has been minimized.

Comment on lines +29 to +30
from mypy.subtypes import is_subtype
from mypy.typeops import get_all_type_vars
Copy link
Contributor Author

Choose a reason for hiding this comment

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

had to put these there due to circular import issues

Comment on lines +45 to +47
# need to manually include these because solve_constraints ignores them
# apparently
constraints.append(Constraint(tvar, SUBTYPE_OF, tvar.upper_bound))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it intentional that solve_constraints ignores the upper bounds of the tvars it solves for?

@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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.

False positive error on **kwargs with Mapping[Literal, Any] type

1 participant