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

Fix for TypeVar bound to a union and methods returning Self #18231

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mariafnafees
Copy link

This pull request resolves #18170 by addressing issues related to TypeVar with Union bounds and methods returning Self. It includes changes to improve type inference and ensuring compatibility with the current logic.

Logic Fixes

  • Enhanced type inference for TypeVar bound to Union types, ensuring proper narrowing for methods returning Self.
  • Updated checkmember.py and typeanal.py to address issues involving get_proper_type() and UnionType.

Enhancements

  • Improved analyze_type_alias() in typeanal.py to correctly handle TypeVar bound to Union, preserving type relationships in Self-returning methods.
  • Refined analyze_member_access() in checkmember.py to ensure accurate handling of Self with TypeVar and Union.

Test Cases

  • Added testTypeVariableSelfUnion and testTypeVarUnionWithSelf to validate the addition of the new features.
  • Enhanced the test suite to include edge cases for TypeVar and Union.

CI Compliance

  • All changes verified to pass runtests.py and other tests in the CI pipeline.

Improvements to Mypy

  • This pull request improves Mypy's ability to handle TypeVar bound to Union types, particularly when interacting with methods returning Self.

The changes ensure:

  • Proper type inference for complex relationships between TypeVar and Union.
  • Accurate narrowing of types based on runtime behavior in methods like clone()

…d analyze_member_access() in checkmember.py to handle TypeVar bound to a Union with methods returning Self, ensuring proper type inference and preserving instance-specific types. Added check-self-type-interface.test to test-data/unit/ to validate the behavior for TypeVar, Union, and Self interactions.
…alyze_type_alias() in typeanal.py to handle TypeVar bound to Union, ensuring proper narrowing of types in methods with Self return types. Added test case testTypeVarUnionWithSelf to check-self-type-interface.test to validate TypeVar and Union inference in Self-returning method contexts.
…errors with get_proper_type() and UnionType. Now passing runtest.py.
Copy link
Contributor

github-actions bot commented Dec 2, 2024

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.

Issue with TypeVar bound to a union and methods returning Self
1 participant