python / python/mypy

Confusing error message for property with @overloaded function

Open
#8,737 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

diagnostics priority-1-normal topic-descriptors topic-error-reporting topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

The following does not seem to work:

from typing import overload


class Example:

	@overload
	def fun(self, p: int) -> int: ...
	@overload
	def fun(self, p: str) -> str: ...

	def fun(self, p=1):
		if isinstance(p, int):
			return 'foo'
		else:
			return 'bar'

	prop = property(fun)

Argument 1 to "property" has incompatible type overloaded function; expected "Optional[Callable[[Any], Any]]"

Is this a bug or expected?
Also, fun(0) returns a string instead of an int as specified in the stub, but mypy doesn't seem to catch this. Again, bug or expected?

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

Start by reproducing the provided Python example and tracing the diagnostics for passing an overloaded method to property and for checking its implementation return type. Done means the expected behavior is established and the resulting diagnostics or checking behavior are covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.