Repository navigation
Report NewType used as the first argument of issubclass() - #22550
Open
StrikeRougeAgent wants to merge 1 commit into
Open
StrikeRougeAgent wants to merge 1 commit into
StrikeRougeAgent wants to merge 1 commit into
Conversation
issubclass() raises TypeError when the class under test is a NewType, but mypy only diagnosed a NewType in the second argument. The argument check does not catch it because a NewType's constructor is accepted where type is expected, so the call type-checked cleanly. Check the first argument of issubclass() for a NewType alongside the existing second-argument checks, and extend the existing isinstance()/ issubclass() NewType test case. Fixes python#22504
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #22504
Problem
issubclass()fails at runtime when its first argument is aNewType:mypy reports a
NewTypein the second argument (Cannot use issubclass() with NewType type), but not in the first, so the broken call type-checks cleanly. Confirmed on master dd565a8 (mypy 2.5.0+dev):Cause
ExpressionChecker.visit_call_expr_innerspecial-casesisinstance()/issubclass()and validates onlye.args[1]:The ordinary argument check does not catch it, because the callable a
NewTypeproduces is accepted wheretypeis expected (t: type = UserIdis also accepted today), soissubclass(UserId, int)reaches no diagnostic at all.Fix
After the existing second-argument checks, validate the first argument of
issubclass()the same way, forNewTypeonly:Deliberately narrow, to keep this to the reported bug:
isinstance()'s first argument is untouched — it takes a value, and aNewTypevalue is just its base value at runtime.Literal,Anyaliases, aliases with type arguments) already producearg-type/miscerrors through normal argument checking, so reusing the whole second-argument loop for argument one would only duplicate messages.issubclass(SomeTypedDict, X)has the same silent pass for the same reason; I left it out of this PR and can send it separately if you want it.Tests
Extended the existing
testNewTypeWithIsInstanceAndIsSubclasscase intest-data/unit/check-newtype.test:Verified by hand as well (single file,
import mod+from mod import W, and aNewTypein both positions):Checks run locally:
pytest -q mypy/test/testcheck.py -k NewTypeWithIsInstance— passpytest -q mypy/test/testcheck.py -n 2—8340 passed, 45 skipped, 7 xfailed(full suite, 200 s)python -m mypy --config-file mypy_self_check.ini mypy/checkexpr.py— cleanblack --check mypy/checkexpr.py,ruff check mypy/checkexpr.py— cleanTesting caveats
testchecksuite plus self-check were run; the rest of mypy's test suite (testcmdline,testfinegrained, stubtest, etc.) was not, since the change is confined to expression checking.AI disclosure: the patch and this description were prepared with an AI coding agent (StrikeRougeAgent, model
deepseek/deepseek-v4.1-flash-1017) under the repo'sCONTRIBUTING.mdLLM guidance. The bug was reproduced on master first, the test case was written to fail without the patch, and I have reviewed the change and can answer questions about it.