From cbf16658826a1a6372d6bebc298505c67f9fc7bc Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Wed, 24 May 2023 23:09:55 -0700 Subject: [PATCH] Support __all__.remove (#15279) See #12582. pyright supports this pattern as well. --- mypy/semanal.py | 8 +++++++- test-data/unit/check-modules.test | 7 ++++--- test-data/unit/fixtures/module_all.pyi | 1 + 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 648852fdecc8..68092e6c3a67 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -4909,7 +4909,7 @@ def visit_call_expr(self, expr: CallExpr) -> None: and isinstance(expr.callee.expr, NameExpr) and expr.callee.expr.name == "__all__" and expr.callee.expr.kind == GDEF - and expr.callee.name in ("append", "extend") + and expr.callee.name in ("append", "extend", "remove") ): if expr.callee.name == "append" and expr.args: self.add_exports(expr.args[0]) @@ -4919,6 +4919,12 @@ def visit_call_expr(self, expr: CallExpr) -> None: and isinstance(expr.args[0], (ListExpr, TupleExpr)) ): self.add_exports(expr.args[0].items) + elif ( + expr.callee.name == "remove" + and expr.args + and isinstance(expr.args[0], StrExpr) + ): + self.all_exports = [n for n in self.all_exports if n != expr.args[0].value] def translate_dict_call(self, call: CallExpr) -> DictExpr | None: """Translate 'dict(x=y, ...)' to {'x': y, ...} and 'dict()' to {}. diff --git a/test-data/unit/check-modules.test b/test-data/unit/check-modules.test index d02dcdc7eb99..8a30237843a5 100644 --- a/test-data/unit/check-modules.test +++ b/test-data/unit/check-modules.test @@ -404,14 +404,15 @@ _ = a _ = b _ = c _ = d -_ = e -_ = f # E: Name "f" is not defined +_ = e # E: Name "e" is not defined +_ = f _ = _g # E: Name "_g" is not defined [file m.py] __all__ = ['a'] __all__ += ('b',) __all__.append('c') -__all__.extend(('d', 'e')) +__all__.extend(('d', 'e', 'f')) +__all__.remove('e') a = b = c = d = e = f = _g = 1 [builtins fixtures/module_all.pyi] diff --git a/test-data/unit/fixtures/module_all.pyi b/test-data/unit/fixtures/module_all.pyi index b14152c7e98f..d6060583b20e 100644 --- a/test-data/unit/fixtures/module_all.pyi +++ b/test-data/unit/fixtures/module_all.pyi @@ -13,6 +13,7 @@ class bool: pass class list(Generic[_T], Sequence[_T]): def append(self, x: _T): pass def extend(self, x: Sequence[_T]): pass + def remove(self, x: _T): pass def __add__(self, rhs: Sequence[_T]) -> list[_T]: pass class tuple(Generic[_T]): pass class ellipsis: pass