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

Fix support of dataclass callable fields #10536

Closed
wants to merge 5 commits into from

Conversation

wyfo
Copy link
Contributor

@wyfo wyfo commented May 26, 2021

Description

This PR is related to #708 and #5485, but fix them for dataclasses only. (Is there really an issue for regular classes?)

mypy currently handle dataclass callable fields as instance methods, as they are annotated at class level the same way a method would be. As a consequence, field node attribute is_initialized_in_class is True.

Here is an illustration of the current mypy behavior, comparing "regular" instance field annotation and dataclass one:

from dataclass import dataclasses
from typing import Callable

class A:
    def __init__():
        self.a: Callable[[int], int] = lambda i: i

@dataclass
class B:
    b: Callable[[int], int] = lambda i: i

A().a(0)  # is_initialized_in_class is False for variable `A().a` -> no error because A().a is instance variable access
B().b(0)  # is_initialized_in_class is True for variable `b().b`  -> error because B().b is a method access

By setting field node is_initialized_in_class to False, the field is then treated the same way it would be when declared with annotated assignment in __init__ method.

A first PR #10292 by @aecay concerning this issue was merged one month ago, but its implementation was more complex and did not fix the bug for frozen dataclasses, so I've reverted it. I hope there is no problem with that.

Test Plan

Add unit tests:

  • mypy/test/testcheck.py::TypeCheckSuite::testDataclassCallableFieldAccess
  • mypy/test/testcheck.py::TypeCheckSuite::testDataclassCallableFieldAssignment

@wyfo
Copy link
Contributor Author

wyfo commented May 26, 2021

I had to modify mypy/test/testdeps.py::GetDependenciesSuite::testDataclassDeps because of a side effect of the code modification. Dependency computation is indeed impacted, but I think this is not a real issue.
In fact, the dependency <m.B.y> -> m appears as soon as y field is used in the module (for example in the test, by adding B(0, 0).y below the class definition), so it should be there when needed.
However, I'm new to mypy code, so I'm not sure of my understanding of dependencies.

@wyfo wyfo changed the title Dataclass callable field Fix support of dataclass callable fields May 27, 2021
@wyfo
Copy link
Contributor Author

wyfo commented May 29, 2021

Should be closed because a more general fix is done #10548

@msullivan msullivan closed this Jun 8, 2021
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.

2 participants