From c667789b1225d13ab6cebfc9a408ac73f2425290 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 22 Jul 2024 21:19:01 +0300 Subject: [PATCH 1/2] Improve `yaml/constructor.pyi` types When updating mypy to 1.11.0 in https://github.com/typeddjango/django-stubs/pull/2269 I noticed this: ``` ______________________________ import_all_modules ______________________________ /home/runner/work/django-stubs/django-stubs/tests/typecheck/test_import_all.yml:2: E pytest_mypy_plugins.utils.TypecheckAssertionError: Output is not expected: E Actual: E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:87: error: Signature of "find_python_module" incompatible with supertype "FullConstructor" [override] (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:87: note: Superclass: (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:87: note: def find_python_module(self, name: Any, mark: Any, unsafe: Any = ...) -> Any (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:87: note: Subclass: (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:87: note: def find_python_module(self, name: Any, mark: Any) -> Any (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:88: error: Signature of "find_python_name" incompatible with supertype "FullConstructor" [override] (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:88: note: Superclass: (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:88: note: def find_python_name(self, name: Any, mark: Any, unsafe: Any = ...) -> Any (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:88: note: Subclass: (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:88: note: def find_python_name(self, name: Any, mark: Any) -> Any (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:89: error: Signature of "make_python_instance" incompatible with supertype "FullConstructor" [override] (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:89: note: Superclass: (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:89: note: def make_python_instance(self, suffix: Any, node: Any, args: Any = ..., kwds: Any = ..., newobj: Any = ..., unsafe: Any = ...) -> Any (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:89: note: Subclass: (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:89: note: def make_python_instance(self, suffix: Any, node: Any, args: Any = ..., kwds: Any = ..., newobj: Any = ...) -> Any (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:90: error: Signature of "set_python_instance_state" incompatible with supertype "FullConstructor" [override] (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:90: note: Superclass: (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:90: note: def set_python_instance_state(self, instance: Any, state: Any, unsafe: bool = ...) -> None (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:90: note: Subclass: (diff) E ../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/yaml-stubs/constructor.pyi:90: note: def set_python_instance_state(self, instance: Any, state: Any) -> Any (diff) E Expected: E (empty) =========================== short test summary info ============================ ``` This happens because `UnsafeConstructor` has unsafe method overrides and new mypy detects this. So, I added just some simple annotations so it can detect it now as well. --- stubs/PyYAML/yaml/constructor.pyi | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/stubs/PyYAML/yaml/constructor.pyi b/stubs/PyYAML/yaml/constructor.pyi index 46374d9061a9..2b0087c17d53 100644 --- a/stubs/PyYAML/yaml/constructor.pyi +++ b/stubs/PyYAML/yaml/constructor.pyi @@ -73,21 +73,21 @@ class FullConstructor(SafeConstructor): def construct_python_long(self, node): ... def construct_python_complex(self, node): ... def construct_python_tuple(self, node): ... - def find_python_module(self, name, mark, unsafe=False): ... - def find_python_name(self, name, mark, unsafe=False): ... + def find_python_module(self, name: str, mark, unsafe: bool = False): ... + def find_python_name(self, name: str, mark, unsafe: bool = False): ... def construct_python_name(self, suffix, node): ... def construct_python_module(self, suffix, node): ... - def make_python_instance(self, suffix, node, args=None, kwds=None, newobj=False, unsafe=False): ... - def set_python_instance_state(self, instance, state, unsafe: bool = False) -> None: ... + def make_python_instance(self, suffix, node, args=None, kwds=None, newobj: bool = False, unsafe: bool = False): ... + def set_python_instance_state(self, instance: Any, state, unsafe: bool = False) -> None: ... def construct_python_object(self, suffix, node): ... def construct_python_object_apply(self, suffix, node, newobj=False): ... def construct_python_object_new(self, suffix, node): ... class UnsafeConstructor(FullConstructor): - def find_python_module(self, name, mark): ... - def find_python_name(self, name, mark): ... - def make_python_instance(self, suffix, node, args=None, kwds=None, newobj=False): ... - def set_python_instance_state(self, instance, state): ... + def find_python_module(self, name: str, mark): ... + def find_python_name(self, name: str, mark): ... + def make_python_instance(self, suffix: str, node, args=None, kwds=None, newobj: bool = False): ... + def set_python_instance_state(self, instance: Any, state): ... class Constructor(SafeConstructor): def construct_python_str(self, node): ... From 303131100b03d00ce460b489af13b93fe8f72ad1 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 22 Jul 2024 21:20:45 +0300 Subject: [PATCH 2/2] Update constructor.pyi --- stubs/PyYAML/yaml/constructor.pyi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stubs/PyYAML/yaml/constructor.pyi b/stubs/PyYAML/yaml/constructor.pyi index 2b0087c17d53..c79680240b31 100644 --- a/stubs/PyYAML/yaml/constructor.pyi +++ b/stubs/PyYAML/yaml/constructor.pyi @@ -84,10 +84,10 @@ class FullConstructor(SafeConstructor): def construct_python_object_new(self, suffix, node): ... class UnsafeConstructor(FullConstructor): - def find_python_module(self, name: str, mark): ... - def find_python_name(self, name: str, mark): ... - def make_python_instance(self, suffix: str, node, args=None, kwds=None, newobj: bool = False): ... - def set_python_instance_state(self, instance: Any, state): ... + def find_python_module(self, name: str, mark): ... # type: ignore[override] + def find_python_name(self, name: str, mark): ... # type: ignore[override] + def make_python_instance(self, suffix: str, node, args=None, kwds=None, newobj: bool = False): ... # type: ignore[override] + def set_python_instance_state(self, instance: Any, state): ... # type: ignore[override] class Constructor(SafeConstructor): def construct_python_str(self, node): ...