Skip to content

Computed "__all__" value results in nothing being exported. #12582

Closed
@mrolle45

Description

@mrolle45

Bug Report

I have two modules:

A.py:
__all__ = 'Foo Bar'.split()
Foo = 0
Bar = 1

B.py:
from A import *
Foo

To Reproduce
mypy .

Expected Behavior
No errors

Actual Behavior

B.py:2: error: Name "Foo" is not defined
Found 1 error in 1 file (checked 2 source files)

Note:
If you make the change:

A.py:
__all__ = ('Foo', 'Bar')
Foo = 0
Bar = 1

Then the output is:

Success: no issues found in 2 source files

In method semanal.SemanticAnalyzer.process__all__(self, s: AssignmentStmt), it only recognizes s.rvalue as being either a TupleExpr or a ListExpr.
In this particular case, it is a CallExpr node. The callee is a MemberExpr with a StrExpr as the object and split as the member name.

Other cases that mypy should recognize would include redefining __all__, as in __all__.append("Baz").

I realize that by design, mypy never executes any user code, so it would be inappropriate to compile and execute the rvalue expression (i.e. there could be something dangerous). However, I think that by analyzing the syntax tree, it can be determined that it has constant value, and what that value is.
I wonder when it is always safe to compile and execute an arbitrary expression. Can you construct some sort of sandbox which is isolated from the current running environment?

At any rate, I'd like to see a robust ability in mypy to recognize expressions which have a constant value.

Your Environment

  • Mypy version used: 0.931
  • Mypy command-line flags: none

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions