`unittest.mock.patch` and `unittest.mock.patch.object` are wrongly typed when new_callable is provided
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 48/100
Rechercherichtung
Überprüfe die im Issue gezeigten Overloads von unittest.mock.patch und patch.object und finde anschließend deren typeshed-Stubs und die zugehörigen Tests. Prüfe die inferierten Typen für standardmäßiges Patching, new, new_callable und Fälle mit nicht aufrufbarem new_callable; fertig ist die Aufgabe, wenn beide APIs nur aufrufbare Factorys akzeptieren und den Typ des erstellten Objekts korrekt inferieren.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Steps to reproduce
from unittest.mock import patch
class NewPrinter:
pass
with patch("pprint.PrettyPrinter", new_callable=lambda: NewPrinter) as patched_printer:
print(patched_printer) # <class '__main__.NewPrinter'>
reveal_type(patched_printer) # "Union[unittest.mock.MagicMock, unittest.mock.AsyncMock]"
Same deal with unittest.mock.patch.object
from unittest.mock import patch
import pprint
class NewPrinter:
...
with patch.object(pprint, "PrettyPrinter", new_callable=lambda: NewPrinter) as patched_printer:
print(patched_printer) # <class '__main__.NewPrinter'>
reveal_type(patched_printer) # "Union[unittest.mock.MagicMock, unittest.mock.AsyncMock]"
Suggested fix
We discuss unittest.mock.patch in the following, but the same principles apply to unittest.mock.patch.object too
Current signatures:
@overload
def __call__(
self,
target: str,
new: _T,
spec: Any | None = ...,
create: bool = ...,
spec_set: Any | None = ...,
autospec: Any | None = ...,
new_callable: Any | None = ...,
**kwargs: Any,
) -> _patch[_T]: ...
@overload
def __call__(
self,
target: str,
*,
spec: Any | None = ...,
create: bool = ...,
spec_set: Any | None = ...,
autospec: Any | None = ...,
new_callable: Any | None = ...,
**kwargs: Any,
) -> _patch_default_new: ...
There are 2 problems here:
new_callableshouldn't beAny | Nonein the first place, it should only acceptCallable| None, as the it will raise if the provided value is not a callablewith patch("pprint.PrettyPrinter", new_callable=1) as patched_printer: # TypeError: 'int' object is not callable- Need to differentiate the case where new_callable is provided and left as default None
- When
new_callable: Callable[..., T], returns_patch[_T] - When
new_callable: None = ..., returns_patch_default_new
- When
Seems like this would do the job:
@overload
def __call__(
self,
target: str,
new: _T,
spec: Any | None = ...,
create: bool = ...,
spec_set: Any | None = ...,
autospec: Any | None = ...,
new_callable: Callable[..., Any] | None = ...,
**kwargs: Any,
) -> _patch[_T]: ...
@overload
def __call__(
self,
target: str,
*,
spec: Any | None = ...,
create: bool = ...,
spec_set: Any | None = ...,
autospec: Any | None = ...,
new_callable: Callable[..., _T],
**kwargs: Any,
) -> _patch[_T]
@overload
def __call__(
self,
target: str,
*,
spec: Any | None = ...,
create: bool = ...,
spec_set: Any | None = ...,
autospec: Any | None = ...,
new_callable: None = ...,
**kwargs: Any,
) -> _patch_default_new: ...
Testing results
from unittest.mock import patch
class NewPrinter:
pass
with patch("pprint.PrettyPrinter") as patched_printer:
print(patched_printer) # <MagicMock name='PrettyPrinter' id='125908774402896'>
reveal_type(patched_printer) # MagicMock | AsyncMock
with patch("pprint.PrettyPrinter", new=NewPrinter) as patched_printer:
print(patched_printer) # <class '__main__.NewPrinter'>
reveal_type(patched_printer) # type[NewPrinter]
with patch("pprint.PrettyPrinter", new_callable=lambda: NewPrinter) as patched_printer:
print(patched_printer) # <class '__main__.NewPrinter'>
reveal_type(patched_printer) # type[NewPrinter]
# No overload variant matches for non-callable type
with patch("pprint.PrettyPrinter", new_callable="non-callable") as patched_printer:
...
Haven't tried unittest.mock.patch.object yet, but probably same thing
Would be great to see what the maintainers think.
I'll dig deeper and try to open a PR, maybe this weekend, will be my first PR in typeshed :)
- Vorherrschende Sprache
- Python
- Sterne
- 5.1k
- Forks
- 2.1k
- Ø Merge
- 2 T. 7 Min.
- Gemergte PRs (30 T.)
- 116
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python/typeshed
-
`networkx.set_edge_attributes` should support scalar value + nameEvtl. vergeben @Fire162 hat das vor 9 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 67/100
Maintainer antworten meist innerhalb von 1 Tag
-
false alarm on decode callEvtl. vergeben @jw9829 hat das vor 36 Tagen übernommen. Offenhelp wanted stubs: false positive
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
python/typeshed#16441 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Don't run pyright basic checks on projects with strict pyright settings in CIEvtl. vergeben @Mohith1612 hat das vor 73 Tagen übernommen. Offenproject: infrastructure
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
python/typeshed#16005 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
stubs: improvement
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
python/typeshed#15548 · 5 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
stubs: improvement
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 64/100
python/typeshed#15529 · 5 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in python/typeshed
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
pytest-dev/pyfakefs#1367 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 2 Tagen
-
IFEvalG exclude_word_harder misses the keyword next to punctuation, at the start or end of the text, and in other letter caseEvtl. vergeben @damienen hat das heute übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
allenai/open-instruct#2151 ·
Maintainer antworten meist innerhalb von 1 Tag
-
feat(plan): support convertorLabels and convertorAffinity in Plan resourceEvtl. vergeben @AmenB hat das heute übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
RedHatQE/openshift-python-wrapper#2849 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
Maintainer antworten meist innerhalb von 25 Tagen
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
libnativeapi/nativeapi#21 ·