Skip to content

Conversation

@chbndrhnns
Copy link
Contributor

@chbndrhnns chbndrhnns commented Dec 5, 2025

Fixes https://youtrack.jetbrains.com/issue/PY-85543


Note

Unwrap typing.NewType to its underlying class in PyTypeChecker matching and add targeted tests (including generics and f-string int() call).

  • Type system:
    • Unwrap PyTypingNewType to its runtime supertype in PyTypeChecker.matchImpl for assignability checks, including handling NewType ancestry and early equality.
  • Tests:
    • Add testNewTypeAcceptedByIntCallInsideFString and testNewTypeWithGenerics in python/testSrc/.../Py3TypeCheckerInspectionTest.java.
    • Add inspection data python/testData/inspections/PyTypeCheckerInspection/NewTypeWithGenerics.py verifying NewType with Dict generics.

Written by Cursor Bugbot for commit f89130f. This will update automatically on new commits. Configure here.

// PY-85543: Treat typing.NewType instances as their underlying runtime type for assignability checks.
// NewType is only a static marker; at runtime it's the supertype, so unwrap it early.
if (actual instanceof PyTypingNewType typingNewType) {
actual = new PyClassTypeImpl(typingNewType.getPyClass(), typingNewType.isDefinition());
Copy link

Choose a reason for hiding this comment

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

Bug: Generic type parameters lost when unwrapping NewType

When actual is a PyTypingNewType, the code converts it to a new PyClassTypeImpl using only getPyClass() and isDefinition(). This discards generic type parameters if the underlying type is a PyCollectionType like Dict[str, int]. For example, NewType("KeyValue", dict[str, int]) would be treated as just dict, causing incorrect type matching where KeyValue values could incorrectly pass checks for dict[str, str]. The underlying classType holds the complete type information including generics, but this information is lost in the conversion.

Fix in Cursor Fix in Web

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in b00a152

@BartvHelvert BartvHelvert added the PyCharm Pull requests for PyCharm label Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PyCharm Pull requests for PyCharm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants