From 3e52d0c1d37344a58c53d4b7a0920b4e38ebedff Mon Sep 17 00:00:00 2001 From: Ali Hamdan Date: Thu, 14 Nov 2024 04:45:47 +0100 Subject: [PATCH] stubgen: do not include mypy generated symbols (#18137) Addresses part of #18081 stubgen still does not handle dataclass transforms correctly but with this change we make sure to never include private mypy generated symbols in the stubs. --- mypy/stubutil.py | 2 ++ test-data/unit/stubgen.test | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/mypy/stubutil.py b/mypy/stubutil.py index 3f917ca7665d..c11843c57f2a 100644 --- a/mypy/stubutil.py +++ b/mypy/stubutil.py @@ -832,6 +832,8 @@ def is_not_in_all(self, name: str) -> bool: return False def is_private_name(self, name: str, fullname: str | None = None) -> bool: + if "__mypy-" in name: + return True # Never include mypy generated symbols if self._include_private: return False if fullname in self.EXTRA_EXPORTED: diff --git a/test-data/unit/stubgen.test b/test-data/unit/stubgen.test index e64c9c66d65d..0801d9a27011 100644 --- a/test-data/unit/stubgen.test +++ b/test-data/unit/stubgen.test @@ -4508,3 +4508,21 @@ class C3[T3 = int]: ... class C4[T4: int | float = int](list[T4]): ... def f5[T5 = int]() -> None: ... + +[case testIgnoreMypyGeneratedMethods_semanal] +# flags: --include-private --python-version=3.13 +from typing_extensions import dataclass_transform + +# TODO: preserve dataclass_transform decorator +@dataclass_transform() +class DCMeta(type): ... +class DC(metaclass=DCMeta): + x: str + +[out] +class DCMeta(type): ... + +class DC(metaclass=DCMeta): + x: str + def __init__(self, x) -> None: ... + def __replace__(self, *, x) -> None: ...