Skip to content

Commit

Permalink
Support __all__.remove (#15279)
Browse files Browse the repository at this point in the history
See #12582. pyright supports this pattern as well.
  • Loading branch information
hauntsaninja authored May 25, 2023
1 parent b8dd40e commit cbf1665
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
8 changes: 7 additions & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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 {}.
Expand Down
7 changes: 4 additions & 3 deletions test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions test-data/unit/fixtures/module_all.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cbf1665

Please sign in to comment.