Repository navigation
Report NewType used as the first argument of issubclass() - #22550
StrikeRougeAgent wants to merge 2 commits 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
ege-arhan
left a comment
There was a problem hiding this comment.
The new check keys on e.callee.name == "issubclass", so it fires when issubclass is a user function that shadows the builtin. False positive that master doesn't have:
from typing import NewType
T = NewType("T", int)
def issubclass(a, b):
return True
issubclass(T, object)master: Success: no issues found in 1 source file
this branch: g.py:7: error: Cannot use issubclass() with NewType type [misc]
The name check at mypy/checkexpr.py:576 is a copy of the one already used for the second argument a few lines above, which has the same hole, so this puts the false positive on a new path instead of closing it. Checking the resolved symbol fixes both: e.callee.fullname == "builtins.issubclass" (the pattern used for is_special_form and friends), plus a test case with a shadowing def issubclass.
One gap the new branch leaves open: an alias in first position is still accepted, Alias = T; issubclass(Alias, object), even though the same alias in second position is reported. Same RefExpr/TypeInfo shape, so likely the alias node needs the check too.
Key the isinstance()/issubclass() argument checks on the resolved callee (builtins.isinstance / builtins.issubclass) instead of the spelled name, so a user-defined function that shadows either builtin no longer triggers the NewType/TypedDict diagnostics. Detect NewType through type aliases as well: a plain `Alias = T` is a TypeAlias whose target is an Instance of the NewType, which the old RefExpr/TypeInfo test missed in both argument positions. PEP 695 aliases are left to the regular argument check, which already reports them. Extend testNewTypeWithIsInstanceAndIsSubclass with alias cases and add testIsInstanceAndIsSubclassShadowed for the shadowing regression.
|
Both points are addressed in 334961a. 1. Resolved callee instead of the name. The gate is actually the 2. NewType aliases. NewType detection now goes through a small One note on the premise: on 221a076 an aliased NewType was also accepted in second position ( Tests. Extended Validation.
Not run: AI disclosure: prepared with StrikeRougeAgent (model |
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.