From fccb483a9fe3416048bbebdd1ec00b165eea72ce Mon Sep 17 00:00:00 2001 From: Mauricio Herrera Cuadra Date: Thu, 10 Nov 2022 16:20:09 -0800 Subject: [PATCH 1/3] PyYAML: Add missing types to _N TypeVar --- stubs/PyYAML/yaml/constructor.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/PyYAML/yaml/constructor.pyi b/stubs/PyYAML/yaml/constructor.pyi index a3187c33c79b..57fcd27e195c 100644 --- a/stubs/PyYAML/yaml/constructor.pyi +++ b/stubs/PyYAML/yaml/constructor.pyi @@ -9,7 +9,7 @@ from yaml.loader import BaseLoader, FullLoader, Loader, SafeLoader, UnsafeLoader from yaml.nodes import MappingNode, Node, ScalarNode, SequenceNode _L = TypeVar("_L", bound=Loader | BaseLoader | FullLoader | SafeLoader | UnsafeLoader) -_N = TypeVar("_N", bound=Node) +_N = TypeVar("_N", bound=Node | MappingNode | ScalarNode | SequenceNode) _Scalar: TypeAlias = str | int | float | bool | None From 9b28f7fc6c1073184e3a49030c1ac436de2dfb3a Mon Sep 17 00:00:00 2001 From: Mauricio Herrera Cuadra Date: Tue, 20 Oct 2026 11:16:18 -0700 Subject: [PATCH 2/3] [PyYAML] Allow specific node types in add_constructor handlers The module-level add_constructor and add_multi_constructor required handlers that accept any Node and every one of Loader, FullLoader and UnsafeLoader. Since Callable parameters are contravariant, handlers annotated with a specific node class (e.g. SequenceNode) or a single loader class were rejected. Use TypeVars bounded by Node and by the loader union instead. Revert the _N bound change from the first commit of this PR: the union was equivalent to Node and _N is not used by these functions. --- .../test_cases/check_add_constructor.py | 34 +++++++++++++++++++ stubs/PyYAML/yaml/__init__.pyi | 12 +++---- stubs/PyYAML/yaml/constructor.pyi | 2 +- 3 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 stubs/PyYAML/@tests/test_cases/check_add_constructor.py diff --git a/stubs/PyYAML/@tests/test_cases/check_add_constructor.py b/stubs/PyYAML/@tests/test_cases/check_add_constructor.py new file mode 100644 index 000000000000..6b38a9b2fcbb --- /dev/null +++ b/stubs/PyYAML/@tests/test_cases/check_add_constructor.py @@ -0,0 +1,34 @@ +from __future__ import annotations + +import yaml + + +def sequence_handler(loader: yaml.Loader, node: yaml.SequenceNode) -> str: + return "" + + +def scalar_handler(loader: yaml.FullLoader, node: yaml.ScalarNode) -> str: + return "" + + +def mapping_handler(loader: yaml.SafeLoader, node: yaml.MappingNode) -> str: + return "" + + +def any_node_handler(loader: yaml.Loader | yaml.FullLoader | yaml.UnsafeLoader, node: yaml.Node) -> str: + return "" + + +def multi_handler(loader: yaml.Loader, tag_suffix: str, node: yaml.MappingNode) -> str: + return "" + + +# Handlers may be annotated with the narrowest loader and node classes they use. +yaml.add_constructor("!seq", sequence_handler) +yaml.add_constructor("!scalar", scalar_handler) +yaml.add_constructor("!any", any_node_handler) +yaml.add_constructor("!map", mapping_handler, Loader=yaml.SafeLoader) +yaml.add_multi_constructor("!multi", multi_handler) + +# A SafeLoader handler can't be registered on the default loaders. +yaml.add_constructor("!map", mapping_handler) # type: ignore diff --git a/stubs/PyYAML/yaml/__init__.pyi b/stubs/PyYAML/yaml/__init__.pyi index 021152a6edb9..70f90017c09d 100644 --- a/stubs/PyYAML/yaml/__init__.pyi +++ b/stubs/PyYAML/yaml/__init__.pyi @@ -22,6 +22,8 @@ from .tokens import * _T = TypeVar("_T") _Constructor = TypeVar("_Constructor", bound=BaseConstructor) +_L = TypeVar("_L", bound=Loader | FullLoader | UnsafeLoader) +_N = TypeVar("_N", bound=Node) _Representer = TypeVar("_Representer", bound=BaseRepresenter) __with_libyaml__: Final[bool] @@ -409,19 +411,17 @@ def add_path_resolver( ) -> None: ... @overload -def add_constructor( - tag: str, constructor: Callable[[Loader | FullLoader | UnsafeLoader, Node], Incomplete], Loader: None = None -) -> None: ... +def add_constructor(tag: str, constructor: Callable[[_L, _N], Incomplete], Loader: None = None) -> None: ... @overload -def add_constructor(tag: str, constructor: Callable[[_Constructor, Node], Incomplete], Loader: type[_Constructor]) -> None: ... +def add_constructor(tag: str, constructor: Callable[[_Constructor, _N], Incomplete], Loader: type[_Constructor]) -> None: ... @overload def add_multi_constructor( - tag_prefix: str, multi_constructor: Callable[[Loader | FullLoader | UnsafeLoader, str, Node], Incomplete], Loader: None = None + tag_prefix: str, multi_constructor: Callable[[_L, str, _N], Incomplete], Loader: None = None ) -> None: ... @overload def add_multi_constructor( - tag_prefix: str, multi_constructor: Callable[[_Constructor, str, Node], Incomplete], Loader: type[_Constructor] + tag_prefix: str, multi_constructor: Callable[[_Constructor, str, _N], Incomplete], Loader: type[_Constructor] ) -> None: ... @overload diff --git a/stubs/PyYAML/yaml/constructor.pyi b/stubs/PyYAML/yaml/constructor.pyi index f7a7d68ecc6d..4c5186840ddd 100644 --- a/stubs/PyYAML/yaml/constructor.pyi +++ b/stubs/PyYAML/yaml/constructor.pyi @@ -14,7 +14,7 @@ from .emitter import _YAMLObject from .loader import _Loader _L = TypeVar("_L", bound=_Loader | _CLoader) -_N = TypeVar("_N", bound=Node | MappingNode | ScalarNode | SequenceNode) +_N = TypeVar("_N", bound=Node) class ConstructorError(MarkedYAMLError): ... From 2a358898f69d2f46c1d2783de1283f3e2ca00932 Mon Sep 17 00:00:00 2001 From: Mauricio Herrera Cuadra Date: Tue, 20 Oct 2026 11:36:20 -0700 Subject: [PATCH 3/3] [PyYAML] Drop test case that CI can't check The pyright regression job installs the dependencies of all stubs into one environment. django-stubs depends on types-PyYAML, so pyright resolves yaml to the published stubs instead of the ones in this repository, and the test case fails against the old signatures even though the fix is in place. --- .../test_cases/check_add_constructor.py | 34 ------------------- 1 file changed, 34 deletions(-) delete mode 100644 stubs/PyYAML/@tests/test_cases/check_add_constructor.py diff --git a/stubs/PyYAML/@tests/test_cases/check_add_constructor.py b/stubs/PyYAML/@tests/test_cases/check_add_constructor.py deleted file mode 100644 index 6b38a9b2fcbb..000000000000 --- a/stubs/PyYAML/@tests/test_cases/check_add_constructor.py +++ /dev/null @@ -1,34 +0,0 @@ -from __future__ import annotations - -import yaml - - -def sequence_handler(loader: yaml.Loader, node: yaml.SequenceNode) -> str: - return "" - - -def scalar_handler(loader: yaml.FullLoader, node: yaml.ScalarNode) -> str: - return "" - - -def mapping_handler(loader: yaml.SafeLoader, node: yaml.MappingNode) -> str: - return "" - - -def any_node_handler(loader: yaml.Loader | yaml.FullLoader | yaml.UnsafeLoader, node: yaml.Node) -> str: - return "" - - -def multi_handler(loader: yaml.Loader, tag_suffix: str, node: yaml.MappingNode) -> str: - return "" - - -# Handlers may be annotated with the narrowest loader and node classes they use. -yaml.add_constructor("!seq", sequence_handler) -yaml.add_constructor("!scalar", scalar_handler) -yaml.add_constructor("!any", any_node_handler) -yaml.add_constructor("!map", mapping_handler, Loader=yaml.SafeLoader) -yaml.add_multi_constructor("!multi", multi_handler) - -# A SafeLoader handler can't be registered on the default loaders. -yaml.add_constructor("!map", mapping_handler) # type: ignore