python / python/mypy

False positives when redefining property in subclass

Open
#5,936 5 comments 16 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

false-positive feature priority-1-normal topic-descriptors
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

I'm running mypy on the following code: (no additional arguments)

class Base:
	def __init__(self, value):
		self._value = value

	@property
	def value(self):
		return self._value

class Sub(Base):
	@Base.value.setter
	def value(self, value):
		self._value = value

And it reports:

inherited_property.py:10: error: "Callable[[Any], Any]" has no attribute "setter"

Line 10 is @Base.value.setter, so it seems to have a problem with referring to a property defined in a different class. Python itself has no problem with this though.

If the property has a setter in the base class, a slightly different error is reported:

class Base:
	def __init__(self, value):
		self._value = value

	@property
	def value(self):
		return self._value

	@value.setter
	def value(self, value):
		self._value = value

class Sub(Base):
	@Base.value.setter
	def value(self, value):
		self._value = value

This results in mypy reporting:

inherited_property.py:14: error: overloaded function has no attribute "setter"

Again the error is reported on the @Base.value.setter line.

I'm using a mypy dev snapshot (0.650+) on Python 3.6.5.

I found the existing issue #220, but that is so generic that I figured it would make sense to open a new separate issue for tracking this specific use case.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the reported diagnostics with the two Python examples, starting with the handling of @property and inherited attribute references in mypy. Trace why Base.value is treated as a callable or overloaded function, then add coverage showing that subclass setters using @Base.value.setter are accepted without errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.