reachable code reported as unreachable using __init__ vs Protocol vs decorators
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 45/100
Direzione di ricerca
Inizia con la riproduzione di pp.py ed esegui mypy pp.py --warn-unreachable usando i controlli mostrati per Protocol, decoratore e wrapped. Confronta l’output a runtime con la diagnostica di mypy sulle istruzioni irraggiungibili; il lavoro è completato quando il ramo Protocol raggiunto non viene più segnalato come irraggiungibile.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Please provide more information to help us understand the issue:
- Are you reporting a bug, or opening a feature request?
bug
- Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
This code decorates init with typeguard, which gives the decorated init a wrapped attribute referring to the original init. This code attempts to unwrap that to get to the original init definition (in my real world use case, to inspect the argument definitions for constructing an instance of a class). At runtime, a Protocol can be used to detect if init has a __wrapped__ attribute, but this does not match up with what mypy believes.
from typing import cast, Any
from typing_extensions import Protocol, runtime_checkable
import typeguard
class Foo():
@typeguard.typechecked
def __init__(self, x: int, y: int,z: int = 3):
self.x = x
self.y = y
self.z = z
print("init")
f = Foo(9,10)
print(f)
func = type(f).__init__
print("__init__ func is {}".format(func))
if hasattr(func, '__wrapped__'):
print("Path 1: is wrapped according to hasattr")
print(cast(Any, type(f).__init__).__wrapped__)
else:
print("Path 1: is not wrapped")
@runtime_checkable
class W(Protocol):
__wrapped__: object
if isinstance(func, W):
print("Path 2: is wrapped according to Protocol - mypy says this is unreachable")
print(type(f).__init__.__wrapped__)
else:
print("Path 2: is not wrapped")
- What is the actual behavior/output?
Execution prints my code's message:Path 2: is wrapped according to Protocol - mypy says this is unreachablefrom line 32.
mypy reports:pp.py:32: error: Statement is unreachablefor that print statement.
Mypy is reporting a statement as unreachable which is actually reached.
-
What is the behavior/output you expect?
Mypy should not report that line as unreachable. -
What are the versions of mypy and Python you are using?
Do you see the same issue after installing mypy from Git master?
I have tried mypy 0.770 and git master:
$ mypy --version
mypy 0.770+dev.52c0a63f4604a2bfa8c0edd6589ab25fab4bdaec
- What are the mypy flags you are using? (For example --strict-optional)
$ mypy pp.py --warn-unreachable
with no mypy.ini
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Merge medio
- 1g 18h
- PR unite (30g)
- 54
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python/mypy
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
bug topic-configuration topic-error-reporting
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
Issue simili
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
zostera/django-bootstrap4#894 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
use-agent-os/agent-os#3276 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
NousResearch/hermes-agent#117848 ·