Possible bug in mypy when attribute and parameter share name on same line.
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
(A clear and concise description of what the bug is.)
I receive the following error from mypy:
api/serializers.py:10: error: Incompatible types in assignment (expression has type "CharField", base class "Field" defined the type as "Callable[..., Any] | str | None") [assignment]
The line in question is:
source = serializers.CharField(source='source_name')
If I change the name of the attribute source to anything else, the error disappears. I think the attribute and parameter are getting mixed up. The error refers to the attribute assignment but reports the type Callable[..., Any] | str | None" which is correct for the parameter.
To Reproduce
I created a project to display this behavior.
https://github.com/btimby/mypy-django-issue
# Ideally, a small sample program that demonstrates the problem.
# Or even better, a reproducible playground link https://mypy-play.net/ (use the "Gist" button)
Expected Behavior
I don't think I should receive and error.
Actual Behavior
I get an error.
Your Environment
Ubuntu linux, python 3.10.2
- Mypy version used: 1.10.0
- Mypy command-line flags:
. - Mypy configuration options from
mypy.ini(and other config files):
[mypy]
plugins =
mypy_django_plugin.main
strict_optional = True
[mypy.plugins.django-stubs]
django_settings_module = issue.settings
- Python version used: 3.10.2
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the report with the linked mypy-django-issue project using mypy 1.10.0, Python 3.10.2, and the shown configuration. Compare the diagnostic for the source = serializers.CharField(source='source_name') line with the version using a different attribute name, then trace the relevant mypy and plugin entry points. Done means the valid declaration no longer produces the incompatible-assignment error and the behavior is covered by a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100