Property annotated as `FunctionType` triggers false unreachable error on `isinstance` check
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
Mypy incorrectly reports an unreachable error when assert isinstance(..., FunctionType) on a @property annotated as returning FunctionType, even though the constructor enforces the invariant.
This is a false positive: no MethodType is involved, and the assertion is trivially true.
Also, the option that enables this error is warn_unreachable. Is this supposed to be a warning instead of an error?
As an aside, while asserting that a defined function (
def func(): ...) is an instance ofFunctionTypeisTrue, if a parameter of a function/method definesfunc: FunctionType, mypy will raise aarg-typeerror at the call site (error: Argument 1 to "FuncWrap" has incompatible type "Callable[[], int]"; expected "FunctionType" [arg-type]). This also seems like a false positive, as raw function definitions are implicitly of typeFunctionType. Not what this bug is about, but something I ran into designing my wrapper.
To Reproduce
from types import FunctionType
class FuncWrap:
def __init__(self, func: object) -> None:
if not isinstance(func, FunctionType):
raise TypeError()
self._func = func
@property
def __func__(self) -> FunctionType:
return self._func
def good() -> int:
return 1
wrapped = FuncWrap(good)
assert isinstance(wrapped.__func__, FunctionType)
If you replace the assert with
assert wrapped.__func__ is good, mypy reports a newcomparison-overlaperror:error: Non-overlapping identity check (left operand type: "MethodType", right operand type: "FunctionType") [comparison-overlap].
Expected Behavior
No errors/warnings.
Actual Behavior
main.py:18: error: Subclass of "MethodType" and "FunctionType" cannot exist: "MethodType" is final [unreachable]
main.py:18: error: Subclass of "MethodType" and "FunctionType" cannot exist: "FunctionType" is final [unreachable]
Found 2 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 2.3.1, 1.20.x, 1.10.x
- Mypy command-line flags: N/A
- Mypy configuration options from
mypy.ini(and other config files):[tool.mypy] cache_dir = "$MYPY_CONFIG_FILE_DIR/.mypy_cache" check_untyped_defs = true color_output = true disallow_any_generics = true disallow_incomplete_defs = true disallow_untyped_decorators = true disallow_untyped_defs = true exclude = [] explicit_package_bases = false files = [ "src/", "tests/" ] implicit_optional = false implicit_reexport = false incremental = true local_partial_types = true mypy_path = "$MYPY_CONFIG_FILE_DIR/src" namespace_packages = false native_parser = true num_workers = 2 plugins = [] python_version = "3.11" show_error_codes = true show_error_context = true strict = true strict_equality = true strict_optional = true warn_no_return = true warn_redundant_casts = true warn_return_any = true warn_unreachable = true warn_unused_configs = true warn_unused_ignores = true - Python version used: 3.[11-14]
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem Reproducer im Issue und verfolge mypy's Erreichbarkeitsanalyse für die isinstance-Assertion, wobei der Behandlung von FunctionType und MethodType besondere Aufmerksamkeit gilt. Füge einen Regressionstest für den mit property annotierten Fall hinzu und überprüfe, dass warn_unreachable keine Fehler oder Warnungen erzeugt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Aktiv
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 50/100