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

Implicit re-exports should not be disabled for installed PEP 561 packages #8754

Closed
GPHemsley opened this issue May 1, 2020 · 5 comments
Closed
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-pep-561

Comments

@GPHemsley
Copy link

GPHemsley commented May 1, 2020

With:

  • Python 3.6.10
  • mypy 0.770
  • requests 2.23.0
  • tenacity 6.2.0

the following code:

#!/usr/bin/env python3

from typing import (
	Any,
	Callable,
)

import requests
import tenacity

# Keep trying the request until we connect.
@tenacity.retry(retry=tenacity.retry_if_exception_type(requests.exceptions.ConnectionError))
def retry_if_connection_error(func: Callable[..., requests.Response], *args: Any, **kwargs: Any) -> requests.Response:
	return func(*args, **kwargs)

# Keep trying the request unless we connect.
@tenacity.retry(retry=tenacity.retry_unless_exception_type(requests.exceptions.ConnectionError))
def retry_unless_connection_error(func: Callable[..., requests.Response], *args: Any, **kwargs: Any) -> requests.Response:
	return func(*args, **kwargs)

produces the following output:

$ mypy --strict scripts/dev_test_mypy_tenacity.py
scripts/dev_test_mypy_tenacity.py:12: error: Module has no attribute "retry_if_exception_type"; maybe "retry_if_exception", "retry_unless_exception_type", or "retry_if_exception_message"?
    @tenacity.retry(retry=tenacity.retry_if_exception_type(requests.exceptions.ConnectionError))
                          ^
scripts/dev_test_mypy_tenacity.py:17: error: Module has no attribute "retry_unless_exception_type"; maybe "retry_if_exception_type"?
    @tenacity.retry(retry=tenacity.retry_unless_exception_type(requests.exceptions.ConnectionError))
                          ^
Found 2 errors in 1 file (checked 1 source file)

Note that each error message suggests an alternative that the other error message insists does not exist.

Tenacity only just added the bare minimum of type hints (jd/tenacity#221), so I don't know if there's some conflict there, but even if that's the case, this seems like a bug in mypy.

Possibly related mypy issues: #8220, #8210, #7125, #7029, #6551, #4930

@JukkaL
Copy link
Collaborator

JukkaL commented May 1, 2020

This seems to be caused by --no-implicit-reexport. Implicit re-exports should always be allowed in PEP 561 packages, but that's not the case right now.

As a workaround, you can run mypy with --strict --implicit-reexport.

@JukkaL JukkaL changed the title mypy reports missing module attribute that it knows is there Implicit re-exports should not be disabled for installed PEP 561 packages May 1, 2020
@JukkaL JukkaL added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-pep-561 labels May 1, 2020
@hauntsaninja
Copy link
Collaborator

Note that #9237 should improve the error message here

pyoor added a commit to MozillaSecurity/autobisect that referenced this issue Jun 24, 2021
pyoor added a commit to MozillaSecurity/autobisect that referenced this issue Jun 24, 2021
* test: add mypy and required types to dev dependencies

* test: add mypy pre-commit hook

* fix: address mypy violations

* test: patch random.choice imported in BuildRange

* fix: add py.typed per pep-561

* test: enable mypy --strict

* fix: apply mypy strict fixes

* feat: update fuzzfetch to 1.3.3 for improved type hints

* fix: disable mypy implicit-reexport checking

This is required until python/mypy#8754 is
fixed.

* fix: additional mypy fixes

* test: minor pre-commit config changes

* fix: catch StopIteration on build_iterator

* test: merge duplicate local repo definitions
@gaborbernat
Copy link
Contributor

gaborbernat commented Aug 2, 2021

Implicit re-exports should always be allowed in PEP 561 packages, but that's not the case right now.

How come @JukkaL? I think implicit re-export should be an opt-in, not always-on feature. I could imagine py.typed file containing some flag to enable/disable this.

@erictraut
Copy link

Implicit re-exports should always be allowed in PEP 561 packages, but that's not the case right now.

This contradicts what (I thought) we had agreed to in the typing-sig discussion on this topic. If a package claims to be py.typed, then it needs to follow typing rules and be explicit about what symbols are exported from each submodule. Pyright assumes that py.typed packages never use implicit re-exports. Here is the guidance we have been providing to package authors, and pyright's implementation is consistent with this guidance. I would encourage mypy to default to the same behavior to provide consistency for package authors.

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)
@hauntsaninja
Copy link
Collaborator

Between #9237 / #13925 / #13967 / #16129 I think we've done everything I'd want to do here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-pep-561
Projects
None yet
Development

No branches or pull requests

5 participants