From e3cf76c9f429460790c822887ae05f476b7ec4b9 Mon Sep 17 00:00:00 2001 From: Maxwell Muoto <41130755+max-muoto@users.noreply.github.com> Date: Mon, 8 Jul 2024 21:35:04 -0500 Subject: [PATCH 1/5] update types.SimpleNamespace --- stdlib/@tests/stubtest_allowlists/common.txt | 1 - stdlib/@tests/stubtest_allowlists/py310.txt | 1 + stdlib/@tests/stubtest_allowlists/py311.txt | 1 + stdlib/@tests/stubtest_allowlists/py38.txt | 1 + stdlib/@tests/stubtest_allowlists/py39.txt | 1 + stdlib/@tests/test_cases/check_types.py | 27 ++++++++++++++++++++ stdlib/types.pyi | 6 ++++- 7 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 stdlib/@tests/test_cases/check_types.py diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index cba2f2149c02..6fc81bc8e4a2 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -500,7 +500,6 @@ types.MethodType.__closure__ # read-only but not actually a property; stubtest types.MethodType.__defaults__ # read-only but not actually a property; stubtest thinks it doesn't exist. types.ModuleType.__dict__ # read-only but not actually a property; stubtest thinks it's a mutable attribute. types.ModuleType.__getattr__ # this doesn't exist at runtime -types.SimpleNamespace.__init__ # class doesn't accept positional arguments but has default C signature # sys attributes that are not always defined sys.gettotalrefcount # Available on python debug builds diff --git a/stdlib/@tests/stubtest_allowlists/py310.txt b/stdlib/@tests/stubtest_allowlists/py310.txt index 25e238c547c4..7555a0ccead9 100644 --- a/stdlib/@tests/stubtest_allowlists/py310.txt +++ b/stdlib/@tests/stubtest_allowlists/py310.txt @@ -52,6 +52,7 @@ tkinter.tix.Shell types.GenericAlias.__getattr__ types.GenericAlias.__mro_entries__ types.GenericAlias.__call__ # Would be complicated to fix properly, Any could silence problems. #6392 +types.SimpleNamespace.__init__ # class doesn't accept positional arguments but has default C signature typing._SpecialForm.__mro_entries__ weakref.ProxyType.__reversed__ # Doesn't really exist builtins.ellipsis # type is not exposed anywhere diff --git a/stdlib/@tests/stubtest_allowlists/py311.txt b/stdlib/@tests/stubtest_allowlists/py311.txt index 25a12ada6c2b..7d1c48f754c9 100644 --- a/stdlib/@tests/stubtest_allowlists/py311.txt +++ b/stdlib/@tests/stubtest_allowlists/py311.txt @@ -96,6 +96,7 @@ _collections_abc.Generator.throw # typing.SupportsRound.__round__ # pos-or-kw at runtime, but we pretend it's pos-only in the stub so that e.g. float.__round__ satisfies the interface types.DynamicClassAttribute..* # In the stub we pretend it's an alias for property, but it has positional-only differences +types.SimpleNamespace.__init__ # class doesn't accept positional arguments but has default C signature # These three have a pos-or-keyword first parameter at runtime, but deliberately have a pos-only first parameter in the stub. #6812 posixpath.join diff --git a/stdlib/@tests/stubtest_allowlists/py38.txt b/stdlib/@tests/stubtest_allowlists/py38.txt index cb1b23328a35..893efaae4a38 100644 --- a/stdlib/@tests/stubtest_allowlists/py38.txt +++ b/stdlib/@tests/stubtest_allowlists/py38.txt @@ -206,6 +206,7 @@ types.GetSetDescriptorType.__get__ types.MemberDescriptorType.__get__ types.MethodDescriptorType.__get__ types.WrapperDescriptorType.__get__ +types.SimpleNamespace.__init__ # class doesn't accept positional arguments but has default C signature multiprocessing.managers.DictProxy.clear multiprocessing.managers.DictProxy.popitem diff --git a/stdlib/@tests/stubtest_allowlists/py39.txt b/stdlib/@tests/stubtest_allowlists/py39.txt index ceac9d170702..4f7d59bc9494 100644 --- a/stdlib/@tests/stubtest_allowlists/py39.txt +++ b/stdlib/@tests/stubtest_allowlists/py39.txt @@ -66,6 +66,7 @@ tkinter.tix.ScrolledWindow tkinter.tix.Shell types.GenericAlias.__getattr__ types.GenericAlias.__call__ # Would be complicated to fix properly, Any could silence problems. #6392 +types.SimpleNamespace.__init__ # class doesn't accept positional arguments but has default C signature weakref.ProxyType.__reversed__ # Doesn't really exist xxsubtype # module missing from the stubs diff --git a/stdlib/@tests/test_cases/check_types.py b/stdlib/@tests/test_cases/check_types.py new file mode 100644 index 000000000000..3654ca86447a --- /dev/null +++ b/stdlib/@tests/test_cases/check_types.py @@ -0,0 +1,27 @@ +import sys +import types +from collections import UserDict + +# test `types.SimpleNamespace` + +# Valid: +types.SimpleNamespace() +types.SimpleNamespace(x=1, y=2) + +if sys.version_info >= (3, 13): + types.SimpleNamespace(()) + types.SimpleNamespace([]) + types.SimpleNamespace([("x", "y"), ("z", 1)]) + types.SimpleNamespace({}) + types.SimpleNamespace(UserDict({"x": 1, "y": 2})) + + +# Invalid: +types.SimpleNamespace(1) # type: ignore +types.SimpleNamespace([1]) # type: ignore +types.SimpleNamespace([["x"]]) # type: ignore +types.SimpleNamespace(**{1: 2}) # type: ignore +types.SimpleNamespace({1: 2}) # type: ignore +types.SimpleNamespace([[1, 2]]) # type: ignore +types.SimpleNamespace(UserDict({1: 2})) # type: ignore +types.SimpleNamespace([[[], 2]]) # type: ignore diff --git a/stdlib/types.pyi b/stdlib/types.pyi index df2d853a630c..11ddf4d7c0f4 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -312,7 +312,11 @@ class MappingProxyType(Mapping[_KT, _VT_co]): class SimpleNamespace: __hash__: ClassVar[None] # type: ignore[assignment] - def __init__(self, **kwargs: Any) -> None: ... + if sys.version_info >= (3, 13): + def __init__(self, mapping_or_iterable: Mapping[str, Any] | Iterable[tuple[str, Any]] = (), /, *kwargs: Any) -> None: ... + else: + def __init__(self, **kwargs: Any) -> None: ... + def __eq__(self, value: object, /) -> bool: ... def __getattribute__(self, name: str, /) -> Any: ... def __setattr__(self, name: str, value: Any, /) -> None: ... From 01b363efab650f2481545f3927632b5703498c2e Mon Sep 17 00:00:00 2001 From: Maxwell Muoto <41130755+max-muoto@users.noreply.github.com> Date: Mon, 8 Jul 2024 21:40:41 -0500 Subject: [PATCH 2/5] Fix mistake --- stdlib/types.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/types.pyi b/stdlib/types.pyi index 11ddf4d7c0f4..a569b55efa23 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -313,7 +313,7 @@ class MappingProxyType(Mapping[_KT, _VT_co]): class SimpleNamespace: __hash__: ClassVar[None] # type: ignore[assignment] if sys.version_info >= (3, 13): - def __init__(self, mapping_or_iterable: Mapping[str, Any] | Iterable[tuple[str, Any]] = (), /, *kwargs: Any) -> None: ... + def __init__(self, mapping_or_iterable: Mapping[str, Any] | Iterable[tuple[str, Any]] = (), /, **kwargs: Any) -> None: ... else: def __init__(self, **kwargs: Any) -> None: ... From 7f79a70485dfdecdca002d1dbb6c9c32dcfc2d9e Mon Sep 17 00:00:00 2001 From: Maxwell Muoto <41130755+max-muoto@users.noreply.github.com> Date: Mon, 8 Jul 2024 21:45:54 -0500 Subject: [PATCH 3/5] Cover 3.12 --- stdlib/@tests/stubtest_allowlists/py312.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/@tests/stubtest_allowlists/py312.txt b/stdlib/@tests/stubtest_allowlists/py312.txt index 469efb38be4a..bb37e9d8fd25 100644 --- a/stdlib/@tests/stubtest_allowlists/py312.txt +++ b/stdlib/@tests/stubtest_allowlists/py312.txt @@ -76,6 +76,7 @@ _collections_abc.Generator.throw # typing.SupportsRound.__round__ # pos-or-kw at runtime, but we pretend it's pos-only in the stub so that e.g. float.__round__ satisfies the interface types.DynamicClassAttribute..* # In the stub we pretend it's an alias for property, but it has positional-only differences +types.SimpleNamespace.__init__ # class doesn't accept positional arguments but has default C signature # These three have a pos-or-keyword first parameter at runtime, but deliberately have a pos-only first parameter in the stub. #6812 posixpath.join From d1e732a96e1642bb7fc592aab3ca5d18e790b1fb Mon Sep 17 00:00:00 2001 From: Maxwell Muoto <41130755+max-muoto@users.noreply.github.com> Date: Tue, 9 Jul 2024 10:15:13 -0500 Subject: [PATCH 4/5] Move to can not be fixed --- stdlib/@tests/stubtest_allowlists/py310.txt | 2 +- stdlib/@tests/stubtest_allowlists/py311.txt | 2 +- stdlib/@tests/stubtest_allowlists/py312.txt | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/py310.txt b/stdlib/@tests/stubtest_allowlists/py310.txt index 7555a0ccead9..8e77280791fa 100644 --- a/stdlib/@tests/stubtest_allowlists/py310.txt +++ b/stdlib/@tests/stubtest_allowlists/py310.txt @@ -52,7 +52,6 @@ tkinter.tix.Shell types.GenericAlias.__getattr__ types.GenericAlias.__mro_entries__ types.GenericAlias.__call__ # Would be complicated to fix properly, Any could silence problems. #6392 -types.SimpleNamespace.__init__ # class doesn't accept positional arguments but has default C signature typing._SpecialForm.__mro_entries__ weakref.ProxyType.__reversed__ # Doesn't really exist builtins.ellipsis # type is not exposed anywhere @@ -250,6 +249,7 @@ pkgutil.ImpImporter\..* pkgutil.ImpLoader\..* types.CodeType.replace # stubtest thinks default values are None but None doesn't work at runtime +types.SimpleNamespace.__init__ # class doesn't accept positional arguments but has default C signature # These enums derive from (str, Enum) pstats.SortKey.__new__ diff --git a/stdlib/@tests/stubtest_allowlists/py311.txt b/stdlib/@tests/stubtest_allowlists/py311.txt index 7d1c48f754c9..aac0d4820b62 100644 --- a/stdlib/@tests/stubtest_allowlists/py311.txt +++ b/stdlib/@tests/stubtest_allowlists/py311.txt @@ -96,7 +96,6 @@ _collections_abc.Generator.throw # typing.SupportsRound.__round__ # pos-or-kw at runtime, but we pretend it's pos-only in the stub so that e.g. float.__round__ satisfies the interface types.DynamicClassAttribute..* # In the stub we pretend it's an alias for property, but it has positional-only differences -types.SimpleNamespace.__init__ # class doesn't accept positional arguments but has default C signature # These three have a pos-or-keyword first parameter at runtime, but deliberately have a pos-only first parameter in the stub. #6812 posixpath.join @@ -136,6 +135,7 @@ inspect._ParameterKind.description # Still exists, but stubtest can't see it os.PathLike.__class_getitem__ # PathLike is a protocol; we don't expect all PathLike classes to implement class_getitem poplib.POP3_SSL.stls # bad declaration of inherited function. See poplib.pyi types.GenericAlias.__call__ # Would be complicated to fix properly, Any could silence problems. #6392 +types.SimpleNamespace.__init__ # class doesn't accept positional arguments but has default C signature types.GenericAlias.__getattr__ types.GenericAlias.__mro_entries__ weakref.ProxyType.__reversed__ # Doesn't really exist diff --git a/stdlib/@tests/stubtest_allowlists/py312.txt b/stdlib/@tests/stubtest_allowlists/py312.txt index bb37e9d8fd25..616fe8cf620e 100644 --- a/stdlib/@tests/stubtest_allowlists/py312.txt +++ b/stdlib/@tests/stubtest_allowlists/py312.txt @@ -76,7 +76,6 @@ _collections_abc.Generator.throw # typing.SupportsRound.__round__ # pos-or-kw at runtime, but we pretend it's pos-only in the stub so that e.g. float.__round__ satisfies the interface types.DynamicClassAttribute..* # In the stub we pretend it's an alias for property, but it has positional-only differences -types.SimpleNamespace.__init__ # class doesn't accept positional arguments but has default C signature # These three have a pos-or-keyword first parameter at runtime, but deliberately have a pos-only first parameter in the stub. #6812 posixpath.join @@ -185,6 +184,9 @@ typing.ParamSpecKwargs.__mro_entries__ typing.TypeVar.__mro_entries__ typing.TypeVarTuple.__mro_entries__ +# class doesn't accept positional arguments but has default C signature +types.SimpleNamespace.__init__ + # TODO: mypy should infer that this attribute is inherited from builtins.type; # why doesn't it infer this? typing.SupportsAbs.__type_params__ From d5d5d6d730221ad980012f4a3076cdca65c2e05e Mon Sep 17 00:00:00 2001 From: Maxwell Muoto <41130755+max-muoto@users.noreply.github.com> Date: Tue, 9 Jul 2024 10:16:00 -0500 Subject: [PATCH 5/5] fix p9 --- stdlib/@tests/stubtest_allowlists/py39.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/@tests/stubtest_allowlists/py39.txt b/stdlib/@tests/stubtest_allowlists/py39.txt index 4f7d59bc9494..5203888299e9 100644 --- a/stdlib/@tests/stubtest_allowlists/py39.txt +++ b/stdlib/@tests/stubtest_allowlists/py39.txt @@ -66,7 +66,6 @@ tkinter.tix.ScrolledWindow tkinter.tix.Shell types.GenericAlias.__getattr__ types.GenericAlias.__call__ # Would be complicated to fix properly, Any could silence problems. #6392 -types.SimpleNamespace.__init__ # class doesn't accept positional arguments but has default C signature weakref.ProxyType.__reversed__ # Doesn't really exist xxsubtype # module missing from the stubs @@ -224,6 +223,7 @@ pkgutil.ImpImporter\..* pkgutil.ImpLoader\..* types.CodeType.replace # stubtest thinks default values are None but None doesn't work at runtime +types.SimpleNamespace.__init__ # class doesn't accept positional arguments but has default C signature # These enums derive from (str, Enum) pstats.SortKey.__new__