Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support __all__.remove #15279

Merged
merged 1 commit into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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