Skip to content

Extend the dataclass plugin to deal with callable properties #10292

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

Merged
merged 2 commits into from
Apr 23, 2021

Conversation

aecay
Copy link
Contributor

@aecay aecay commented Apr 7, 2021

Description

At runtime, the callable properties of dataclasses are handled in the
way one would expect: they are not passed a self argument. Mypy,
however, just sees them as callable class attributes and generates
errors about missing arguments. This is a special case of what is
discussed in #708. I donʼt have a general solution for that problem,
but for dataclasses, I can fix it by automatically converting the
callable entries in a data class into (settable) properties. That makes
them work properly via-a-vis the typechecker.

(Explain how this PR changes mypy.)

Test Plan

Added unit tests which verify:

  • callable dataclass properties no longer generate mypy errors about arity mismatches
    • neither as class init arguments
    • nor via property assignment
    • nor when calling a property on an instance
  • callable dataclass properties reject incompatible types
    • as class init arguments
    • and via property assignment

At runtime, the callable properties of dataclasses are handled in the
way one would expect: they are not passed a `self` argument.  Mypy,
however, just sees them as callable class attributes and generates
errors about missing arguments.  This is a special case of what is
discussed in python#708.  I donʼt have a general solution for that problem,
but for dataclasses, I can fix it by automatically converting the
callable entries in a data class into (settable) properties.  That makes
them work properly via-a-vis the typechecker.
aecay added a commit to sky-uk/mite that referenced this pull request Apr 7, 2021
this is (hopefully) temporary until
python/mypy#10292 is merged
aecay added a commit to sky-uk/mite that referenced this pull request Apr 7, 2021
this is (hopefully) temporary until
python/mypy#10292 is merged
@aecay
Copy link
Contributor Author

aecay commented Apr 8, 2021

The travis failure is in the python3.5 unit test job, which fails before the tests even run:

 Could not find a version that satisfies the requirement typing (from versions: )
No matching distribution found for typing

This same job passed on fb07856, and I can't see where the changes in 6149640 would have caused it to start failing... So it looks like a transient issue. When I try to rerun the job on Travis, I get an error ("could not rerun job") -- maybe a permissions issue? In any case, I figured it makes sense to wait for reviews and maybe a little help coaxing the CI to rerun rather than keep bashing away at it for the moment.

@JelleZijlstra
Copy link
Member

I used the powers vested in me to click the "Re-run job button" in Travis. For future reference, closing and reopening the PR tends to work too.

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Looks good.

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