-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[1.16 regression] narrowing on is_dataclass #19139
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
Comments
This types better on 1.16 (will need a few more adjustments when it comes out, but this is closer). See hauntsaninja/mypy_primer#174 and python/mypy#19139. Signed-off-by: Henry Schreiner <[email protected]>
from typing import TypeIs
class A: ...
def is_a(o: object) -> TypeIs[type[A] | A]:
return isinstance(o, int)
raw_target: object
if is_a(raw_target) and isinstance(raw_target, type):
reveal_type(raw_target) # N: Revealed type is "builtins.type"
if isinstance(raw_target, type) and is_a(raw_target):
reveal_type(raw_target) # N: Revealed type is "Union[type[__main__.A], __main__.A]"
My MRE exhibits the same behaviour on 1.15.0, so it isn't the same. And this is correct - This issue bisects to #17678; # HACK: `obj: Never` typing matches if object argument is using `Any` type.
@overload
def is_dataclass(obj: Never) -> TypeIs[DataclassInstance | type[DataclassInstance]]: ... # type: ignore[narrowed-type-not-subtype] # pyright: ignore[reportGeneralTypeIssues]
@overload
def is_dataclass(obj: type) -> TypeIs[type[DataclassInstance]]: ...
@overload
def is_dataclass(obj: object) -> TypeIs[DataclassInstance | type[DataclassInstance]]: ... And this looks like a bug - after first |
@hauntsaninja could you please create a label for PEP 742 / TypeIs? |
I get it now. When Splitting the ladder works: import dataclasses
raw_target: object
if isinstance(raw_target, type):
if dataclasses.is_dataclass(raw_target):
reveal_type(raw_target) # N: Revealed type is "type[_typeshed.DataclassInstance]" |
…hon#19161) This reverts commit 43ea203 (python#17678). The commit caused a regression (python#19139). If we can't fix the regression soon enough, reverting the original change temporarily will at least unblock the mypy public release. The reverted PR can be merged again once the regression is fixed.
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
If you use
isinstance(item, type)
, this used to be used in conjunction withdataclasses.is_dataclass
to identify types and instances. It's the recommendation in the CPython documentation, in fact: https://docs.python.org/3/library/dataclasses.html#dataclasses.is_dataclass. However, the latests MyPy no longer narrows this correctly (from what I can tell, unless I'm doing something wrong), and still thinks I might have an instance, which then breaks later when creating an instance, etc.Note that I can flip the order, in which case I just get
builtins.type
for both versions. This isn't enough to fix it for some of the more complex usages in scikit-build-core, though.To Reproduce
Expected Behavior
This is from 1.15:
Actual Behavior
Your Environment
uvx --from git+https://github.com/python/mypy mypy tmp.py
was used, so latest commit, also latest release 1.15 version for comparison.Noticed in hauntsaninja/mypy_primer#174.
The text was updated successfully, but these errors were encountered: