Skip to content

Commit cbf1665

Browse files
authored
Support __all__.remove (#15279)
See #12582. pyright supports this pattern as well.
1 parent b8dd40e commit cbf1665

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

mypy/semanal.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -4909,7 +4909,7 @@ def visit_call_expr(self, expr: CallExpr) -> None:
49094909
and isinstance(expr.callee.expr, NameExpr)
49104910
and expr.callee.expr.name == "__all__"
49114911
and expr.callee.expr.kind == GDEF
4912-
and expr.callee.name in ("append", "extend")
4912+
and expr.callee.name in ("append", "extend", "remove")
49134913
):
49144914
if expr.callee.name == "append" and expr.args:
49154915
self.add_exports(expr.args[0])
@@ -4919,6 +4919,12 @@ def visit_call_expr(self, expr: CallExpr) -> None:
49194919
and isinstance(expr.args[0], (ListExpr, TupleExpr))
49204920
):
49214921
self.add_exports(expr.args[0].items)
4922+
elif (
4923+
expr.callee.name == "remove"
4924+
and expr.args
4925+
and isinstance(expr.args[0], StrExpr)
4926+
):
4927+
self.all_exports = [n for n in self.all_exports if n != expr.args[0].value]
49224928

49234929
def translate_dict_call(self, call: CallExpr) -> DictExpr | None:
49244930
"""Translate 'dict(x=y, ...)' to {'x': y, ...} and 'dict()' to {}.

test-data/unit/check-modules.test

+4-3
Original file line numberDiff line numberDiff line change
@@ -404,14 +404,15 @@ _ = a
404404
_ = b
405405
_ = c
406406
_ = d
407-
_ = e
408-
_ = f # E: Name "f" is not defined
407+
_ = e # E: Name "e" is not defined
408+
_ = f
409409
_ = _g # E: Name "_g" is not defined
410410
[file m.py]
411411
__all__ = ['a']
412412
__all__ += ('b',)
413413
__all__.append('c')
414-
__all__.extend(('d', 'e'))
414+
__all__.extend(('d', 'e', 'f'))
415+
__all__.remove('e')
415416

416417
a = b = c = d = e = f = _g = 1
417418
[builtins fixtures/module_all.pyi]

test-data/unit/fixtures/module_all.pyi

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class bool: pass
1313
class list(Generic[_T], Sequence[_T]):
1414
def append(self, x: _T): pass
1515
def extend(self, x: Sequence[_T]): pass
16+
def remove(self, x: _T): pass
1617
def __add__(self, rhs: Sequence[_T]) -> list[_T]: pass
1718
class tuple(Generic[_T]): pass
1819
class ellipsis: pass

0 commit comments

Comments
 (0)