Skip to content
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

dataclasses.replace: fall through to typeshed sig #15962

Merged
merged 3 commits into from
Aug 29, 2023

Conversation

ikonst
Copy link
Contributor

@ikonst ikonst commented Aug 26, 2023

If the dataclasses plugin cannot determine a signature for dataclasses.replace, it should not report an error. The underlying typeshed signature will get a shot at verifying the type and reporting an error, and it would enable the following pattern (without typing replace's kwargs, though):

from dataclasses import is_dataclass, replace

def foo(x: object):
  if is_dataclass(x) and not isinstance(x, type):
#    ^^^^^^^^^^^^
#    this is a type-guard in the typeshed
#
    y = replace(x)

@github-actions

This comment has been minimized.

@ikonst ikonst force-pushed the remove-_fail_not_dataclass branch 2 times, most recently from e91d2da to a2f3780 Compare August 26, 2023 05:51
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Generally looks good, just one question :)

@@ -853,7 +853,7 @@ class Person:
name: str

p = Person('John')
y = replace(p, name='Bob') # E: Argument 1 to "replace" has incompatible type "Person"; expected a dataclass
y = replace(p, name='Bob')
Copy link
Member

Choose a reason for hiding this comment

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

I think that this can potentially introduce some new weak spots, but on other hand it can eventually work :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a false positive but it's due to #15974.

# error before type-guard
y = replace(x) # E: Value of type variable "_DataclassT" of "replace" cannot be "object"
# no error after type-guard
if is_dataclass(x) and not isinstance(x, type):
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to test just if is_dataclass(x):?

Copy link
Contributor Author

@ikonst ikonst Aug 27, 2023

Choose a reason for hiding this comment

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

It's because type is an object, and is_dataclass on an object either means it's a dataclass instance or type.

I'll improve the test to clarify that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

vision (https://github.com/pytorch/vision): typechecking got 1.37x slower (36.8s -> 50.5s)
(Performance measurements are based on a single noisy sample)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants