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

Incorrect "Module ... has no attribute" #7423

Closed
be9 opened this issue Aug 30, 2019 · 5 comments
Closed

Incorrect "Module ... has no attribute" #7423

be9 opened this issue Aug 30, 2019 · 5 comments

Comments

@be9
Copy link

be9 commented Aug 30, 2019

I've created a full repro here.

In short, there's a main module which imports some names from another package:

# planner/web.py
from planner.scheduling import RandyScheduleBuilder, ScheduleBuilder, book_campaign, cancel_campaign, reserve_campaign
#  ...
# planner/scheduling/__init__.py
from planner.scheduling.reserve import (
    book_campaign,
    cancel_campaign,
    reserve_campaign,
)
from planner.scheduling.make_schedule import (
    LowLevelSchedulingStats,
    make_schedule,
)

from planner.scheduling.lowlevel.base import ScheduleBuilder, ScheduleBuilderMaker
from planner.scheduling.lowlevel.randy import RandyScheduleBuilder

All these names are correct, however mypy --strict -p planner shows errors:

planner/web.py:1: error: Module 'planner.scheduling' has no attribute 'RandyScheduleBuilder'; maybe "ScheduleBuilder"?
planner/web.py:1: error: Module 'planner.scheduling' has no attribute 'ScheduleBuilder'; maybe "ScheduleBuilderMaker" or "RandyScheduleBuilder"?
planner/web.py:1: error: Module 'planner.scheduling' has no attribute 'book_campaign'
planner/web.py:1: error: Module 'planner.scheduling' has no attribute 'cancel_campaign'
planner/web.py:1: error: Module 'planner.scheduling' has no attribute 'reserve_campaign'
@JukkaL
Copy link
Collaborator

JukkaL commented Aug 30, 2019

--strict defines --no-implicit-reexport, which requires exported imported names to be imported using from foo import bar as bar. The error message is confusing, however (#7251).

@JukkaL JukkaL closed this as completed Aug 30, 2019
@be9
Copy link
Author

be9 commented Aug 30, 2019

@JukkaL yes, indeed, removing --strict removes the errors. However, if I keep using --strict and define __all__, errors are still there:

from planner.scheduling.reserve import (
    book_campaign,
    cancel_campaign,
    reserve_campaign,
)
from planner.scheduling.make_schedule import (
    LowLevelSchedulingStats,
    make_schedule,
)

from planner.scheduling.lowlevel.base import ScheduleBuilder, ScheduleBuilderMaker
from planner.scheduling.lowlevel.randy import RandyScheduleBuilder


__all__ = [
    'book_campaign',
    'cancel_campaign',
    'reserve_campaign',
    'LowLevelSchedulingStats',
    'make_schedule',
    'ScheduleBuilder',
    'ScheduleBuilderMaker',
    'RandyScheduleBuilder',
]

The docs say that __all__ should work.

P.S. import ... as ... works though.

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 30, 2019

__all__ works on master for me. Can you try the GitHub mypy master?

@be9
Copy link
Author

be9 commented Aug 31, 2019

@JukkaL yes, __all__ on master works for me too.

ahawker added a commit to ahawker/ulid that referenced this issue Jun 17, 2020
This is required because mypy --strict uses --no-implicit-reexport.
See: python/mypy#7423
mariuszskon added a commit to mariuszskon/autorop that referenced this issue Sep 28, 2020
Thanks to python/mypy#7423 I understood that the problem was in my relationship with mypy.
@stur86
Copy link

stur86 commented Apr 27, 2022

Resurrecting this old issue - I'm having this problem now, with version 0.942. Importing a module with __all__ defined explicitly and then accessing __all__ still causes a MyPy warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants