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

Ensure subtyping works with erased typevartuples in callables #18408

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

A5rocks
Copy link
Contributor

@A5rocks A5rocks commented Jan 2, 2025

Fixes #18407

Ironically this is logic directly taken from my other PR #17512. I present this as a seperate PR because now I have a test case for the easier logic -- I'm not pleased with the rest and I think it can be done better (once I have time to think about how to work with constraint logic for a more general solution).

The reason behind these subtyping changes are to allow this subtyping check to pass:

mypy/mypy/constraints.py

Lines 424 to 427 in e05770d

if direction == SUPERTYPE_OF and not mypy.subtypes.is_subtype(
actual, erase_typevars(template)
):
return None

After this, everything works as is typical. I'm pretty sure the reason this passes without the union is because constraints with a callable template don't check subtyping.

Copy link
Contributor

github-actions bot commented Jan 2, 2025

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

)
else:
trivial_varargs = False

if (
right.arg_kinds == [ARG_STAR]
and isinstance(get_proper_type(right.arg_types[0]), AnyType)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm pretty sure this check is redundant. It would require updating above to just handle *args: Any and handling tuple[Any, ...] here:

mypy/mypy/types.py

Lines 2164 to 2168 in e05770d

if not isinstance(unpacked, TupleType):
# Note that we don't normalize *args: *tuple[X, ...] -> *args: X,
# this should be done once in semanal_typeargs.py for user-defined types,
# and we ourselves rarely construct such type.
return self

I'm just not sure because the comment says the form isn't constructed by mypy but it's the constructed form of any erased typevartuple? I also don't like treating any given *args: Any as finite length, but I think that ship has sailed.

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's inference gets confused about unions with typevartuple
1 participant