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

BUG: Stubgen removed needed import incorrectly #13661

Closed
matejsp opened this issue Sep 14, 2022 · 0 comments · Fixed by #15624
Closed

BUG: Stubgen removed needed import incorrectly #13661

matejsp opened this issue Sep 14, 2022 · 0 comments · Fixed by #15624
Labels
bug mypy got something wrong topic-stubgen

Comments

@matejsp
Copy link

matejsp commented Sep 14, 2022

Bug Report

Using latest mypy 0.971 stub is incorrectly generated (missing import in case of multiple "same imports").

from __future__ import absolute_import

import typing

import stubgenbug.common.circuit_breaker
import stubgenbug.common.typing


class HttpClient(object):
    def __init__(
        self,
        url,  # type: stubgenbug.common.typing.String
        username=None,  # type: typing.Optional[stubgenbug.common.typing.String]
        password=None,  # type: typing.Optional[stubgenbug.common.typing.String]
        breaker=None,  # type: typing.Optional[stubgenbug.common.circuit_breaker.CircuitBreaker]
    ):
        # type: (...) -> None
        pass

Stubgen generates:

import stubgenbug.common.typing
import typing

class HttpClient:
    def __init__(
        self, 
        url: stubgenbug.common.typing.String, 
        username: typing.Optional[stubgenbug.common.typing.String] = ..., 
        password: typing.Optional[stubgenbug.common.typing.String] = ..., 
        breaker: typing.Optional[stubgenbug.common.circuit_breaker.CircuitBreaker] = ...
    ) -> None: ...

Since stubgenbug.common.circuit_breaker was lost in the process stubgenbug.common.circuit_breaker.CircuitBreaker is not resolved.

I am attaching the minimal example.
stubgenbug.tar.gz

To Reproduce

Extract attached file and run stubgen stubgenbug -o stubgenbug/out

Expected Behavior

import stubgenbug.common.circuit_breaker should stay in pyi

Your Environment

  • Mypy version used: 0.971
  • Mypy command-line flags: stubgen stubgenbug -o stubgenbug/out
  • Mypy configuration options from mypy.ini (and other config files): /
  • Python version used: 3.8.13
  • Operating system and version: MacOSX 12.5.1
@matejsp matejsp added the bug mypy got something wrong label Sep 14, 2022
@matejsp matejsp changed the title BUG:Stubgen removed needed import incorrectly BUG: Stubgen removed needed import incorrectly Sep 14, 2022
hamdanal added a commit to hamdanal/mypy that referenced this issue Jul 8, 2023
* Fix handling of nested imports
  Instead of assuming that a name is imported from a top level package,
  look in the imports for this name starting from the parent submodule
  up until the import is found
* Fix "from imports" getting rexported unnecessarily
* Fix import sorting when having import aliases

Fixes python#13661
Fixes python#7006
hauntsaninja pushed a commit that referenced this issue Sep 25, 2023
* Fix handling of nested imports.
Instead of assuming that a name is imported from a top level package,
look in the imports for this name starting from the parent submodule up
until the import is found
* Fix "from imports" getting reexported unnecessarily
* Fix import sorting when having import aliases

Fixes #13661
Fixes #7006
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-stubgen
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants