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

Maybe __all__ should be added explicitly in stubgen #10314

Closed
wrvsrx opened this issue Apr 12, 2021 · 0 comments · Fixed by #16356
Closed

Maybe __all__ should be added explicitly in stubgen #10314

wrvsrx opened this issue Apr 12, 2021 · 0 comments · Fixed by #16356

Comments

@wrvsrx
Copy link

wrvsrx commented Apr 12, 2021

Feature

(A clear and concise description of your feature proposal.)

Maybe __all__ should be added explicitly in stubgen.

Pitch

(Please explain why this feature should be implemented and how it would be used. Add examples, if applicable.)

Adding __all__ to type stubs explicitly can avoid some error. For example:

├── test
│   └── init.py

__init__.py:

class Test:
    pass

__all__ = []

The type stub file generated by stubgen is
__init__.pyi:

class Test: ...

When using from .test import * to import test as a submodule, Test will be imported according to the type stub, while in fact Test should not be used.

A Possible Implement

Replace output(self) in stubgen.py by following implement:

    def output(self) -> str:
        """Return the text for the stub."""
        imports = ''
        if self._import_lines:
            imports += ''.join(self._import_lines)
        imports += ''.join(self.import_tracker.import_lines())
        if imports and self._output:
            imports += '\n'
        imports += ''.join(self._output)
        """export __all__ when it's defined"""
        if imports and self._all_ != None:
            imports += '\n'
        if self._all_ != None:
            imports += '__all__ = ['
            for name in self._all_:
                imports += ''.join(('\"', name, '\", '))
            imports += ']'
        return imports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants