More detailed checking of type objects in stubtest #18251
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.
This uses
checkmember.type_object_type
and context to produce better types of type objects.I did this work while doing a deep dive on
argparse.ArgumentParser.__init__
, which is on the allowlist in typeshed with the note:stubtest doesn't recognise the runtime default (a class) as being compatible with a callback protocol (the stub annotation)
Mypy proper has no problem with this, and after investigation the primary difference seemed to be that stubtest looked up the type of the class and got
type
while Mypy proper usedcheckmember.type_object_type
to convert the class into a callable type. This worked to clearargparse.ArgumentParser.__init__
, but generated all kinds of new errors at first. After some cleanup, I still had about as many new errors as errors fixed. Several of these were an instance of #3737, so I special-cased them to skip the new logic.At this point, this MR clears five allowlist entries from typeshed standard library:
It introduces four new errors:
Of these, I think that:
ctypes.CDLL._func_restype_
is a genuine error in the stubs (we have_func_restype_: ClassVar[_CDataType]
and it should be_func_restype_: ClassVar[type[_CDataType]]
). `ctypes.c_time_t
looks like a genuine error. We havec_time_t: type[c_int32 | c_int64]
which is fine, but the issue is that in the stubsc_int32
andc_int64
are both unique classes and at runtime they're each an alias for one ofc_short
,c_int
,c_long
, orc_longlong
. We can clear it by expanding the union to include those additional types, but I'm 100% certain if that's better or not.unittest.TextTestRunner.resultclass
andunittest.runner.TextTestRunner.resultclass
are the same error. I looked at it for a while and I can't figure out what the issue is. We haveresultclass: _ResultClassType
where that's the type alias:and the runtime default is
TextTestResult
, which hasIt's a complicated set of type aliases and type variables, and I think it's probably likely that there's something real here, but I can't spot it.