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

Does not recognize reexported modules #3409

Closed
balazser opened this issue Apr 29, 2022 · 5 comments
Closed

Does not recognize reexported modules #3409

balazser opened this issue Apr 29, 2022 · 5 comments
Labels
as designed Not a bug, working as intended

Comments

@balazser
Copy link

Describe the bug
Pyright shows a not exported from module error for modules that are correctly reexported, although XMLFile imported in the same way seems to be correct.

To Reproduce
Install Twisted==22.2.0
Import the following modules

from twisted.web.template import Element"Element" is not exported from module "twisted.web._template_util"    Import from "twisted.web._element" instead
from twisted.web.template import XMLFile
from twisted.web.template import renderer"renderer" is not exported from module "twisted.web._template_util"    Import from "twisted.web._element" instead

Expected behavior
No error is shown

Screenshots or Code
Twisted template modules are reexported in /usr/local/pyenv/versions/3.10.2/envs/foo/lib/python3.10/site-packages/twisted/web/template.py

from ._template_util import (
    CDATA,
    TEMPLATE_NAMESPACE,
    VALID_HTML_TAG_NAMES,
    Comment,
    Element,
    Flattenable,
    Tag,
    TagLoader,
    XMLFile,
    XMLString,
    flatten,
    flattenString,
    renderElement,
    renderer,
    slot,
    tags,
)
@erictraut
Copy link
Collaborator

These symbols are imported but not re-exported. Since this is a "py.typed" library, symbols must be explicitly re-exported if that is the intent. For additional details about how pyright treats "py.typed" libraries, refer to this documentation.

I'm going to close this because the current behavior is "as designed". Please file bugs with the maintainers of this library if you think that these symbols should be explicitly re-exported through this submodule.

@erictraut erictraut added the as designed Not a bug, working as intended label Apr 29, 2022
@balazser
Copy link
Author

Thank you for your reply.
The module that I linked defines the __all__ object with the failing modules explicitly re-exported. Why does pyright still fail? 🤔

https://github.com/twisted/twisted/blob/3bbe558df65181ed455b0c5cc609c0131d68d265/src/twisted/web/template.py#L22

__all__ = [
    "TEMPLATE_NAMESPACE",
    "VALID_HTML_TAG_NAMES",
    "Element",
    "Flattenable",
    "TagLoader",
    "XMLString",
    "XMLFile",
    "renderer",
    "flatten",
    "flattenString",
    "tags",
    "Comment",
    "CDATA",
    "Tag",
    "slot",
    "CharRef",
    "renderElement",
]

According to the doc that you linked and mypy

A module can expose an all symbol at the module level that provides a list of names that are considered part of the interface. The all symbol indicates which symbols are included in a wildcard import. All symbols included in the all list are considered public even if the other rules above would otherwise indicate that they were private. For example, this allows symbols whose names begin with an underscore to be included in the interface.

@erictraut
Copy link
Collaborator

While template.py includes Element in its __all__ statement, it is attempting to import Element from another module (_template_util) that itself imports Element from yet another module (twisted.web._element), and _template_util doesn't re-export it.

@erictraut
Copy link
Collaborator

The command-line version of pyright has an option called --verifytypes that can be used to verify the "type completeness" of a "py.typed" library. Libraries that contain a "py.typed" marker are asserting that they contain complete and correct type information.

When I run pyright --verifytypes twisted, here is the summary I receive:

Symbols exported by "twisted": 30337
  With known type: 5710
  With ambiguous type: 2696
  With unknown type: 21931

Type completeness score: 18.8%

That unfortunately means this library has a long way to go before it is properly typed.

You may find that it's best to manually delete the "py.typed" marker for the library because it's probably doing more harm than good at this point. When the "py.typed" marker is present, pyright treats the library more strictly under the assumption that the library maintainers have done the additional work to provide accurate type information. When "py.typed" isn't present, pyright will loosen its rules and attempt to infer type information from the available source code if you enable the useTypesFromLibraryCode configuration option or pass --lib to the command line.

@balazser
Copy link
Author

balazser commented May 2, 2022

Now I see why it fails. Thank you erictraut for explaining it. I appreciate it. 🙏

Jeremiah-England added a commit to Jeremiah-England/aw-client that referenced this issue Sep 4, 2023
It looks like MyPy and Pyright currently require imports like these to
be re-exported in order to be used. Doing this raises a lint error for
me due to Pyright:

```python
import aw_client

aw_client.ActivityWatchClient()  # "ActivityWatchClient" is not exported from module "aw_client"
```

Ref: https://github.com/microsoft/pyright/blob/main/docs/typed-libraries.md#library-interface
Ref: microsoft/pyright#3409
Ref: python/mypy#8754 (comment)
ErikBjare pushed a commit to ActivityWatch/aw-client that referenced this issue Sep 4, 2023
It looks like MyPy and Pyright currently require imports like these to
be re-exported in order to be used. Doing this raises a lint error for
me due to Pyright:

```python
import aw_client

aw_client.ActivityWatchClient()  # "ActivityWatchClient" is not exported from module "aw_client"
```

Ref: https://github.com/microsoft/pyright/blob/main/docs/typed-libraries.md#library-interface
Ref: microsoft/pyright#3409
Ref: python/mypy#8754 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended
Projects
None yet
Development

No branches or pull requests

2 participants