GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve AttributeError message for partially initialized module · Issue #77418 · python/cpython

Improve AttributeError message for partially initialized module #77418

New issue
Copy link
New issue
Copy link
Closed
Closed
Improve AttributeError message for partially initialized module#77418
Copy link
Labels
3.8 (EOL)end of lifeend of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement

Description

@serhiy-storchaka
serhiy-storchaka
opened on Apr 6, 2018
Issue body actions
BPO 33237
Nosy @brettcannon, @ncoghlan, @ericsnowcurrently, @serhiy-storchaka
PRs
  • bpo-33237: Improve AttributeError message for partially initialized module. #6398
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-10-30.11:21:29.153>
    created_at = <Date 2018-04-06.15:43:32.578>
    labels = ['interpreter-core', 'type-feature', '3.8']
    title = 'Improve AttributeError message for partially initialized module'
    updated_at = <Date 2018-10-30.11:21:29.152>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2018-10-30.11:21:29.152>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-10-30.11:21:29.153>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2018-04-06.15:43:32.578>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33237
    keywords = ['patch']
    message_count = 8.0
    messages = ['315019', '315020', '315071', '315076', '315136', '315166', '328131', '328895']
    nosy_count = 4.0
    nosy_names = ['brett.cannon', 'ncoghlan', 'eric.snow', 'serhiy.storchaka']
    pr_nums = ['6398']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue33237'
    versions = ['Python 3.8']

    Activity

    1. serhiy-storchaka commented on Apr 6, 2018

      @serhiy-storchaka
      serhiy-storchaka
      on Apr 6, 2018
      MemberAuthor
      More actions

      Cyclic import usually leads to an AttributeError "module 'spam' has no attribute 'ham'" which usually is confusing because in normal case 'spam.ham' exists, and the user can have no ideas why it is disappeared.

      The proposed PR allows to specialize the AttributeError message for partially initialized module. Any suggestions about the error message?

    2. serhiy-storchaka
      added
      3.8 (EOL)end of lifeend of life
      interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
      type-featureA feature request or enhancementA feature request or enhancement
      on Apr 6, 2018
    3. ncoghlan commented on Apr 6, 2018

      @ncoghlan
      ncoghlan
      on Apr 6, 2018
      Contributor
      More actions

      While I like the idea of this change, the "partially initialized" addition is fairly subtle, and relatively easy to miss.

      Perhaps append "(most likely due to a circular import)" to the partially initialized case?:

      AttributeError: partially initialized "module 'spam' has no attribute 'ham' (most likely due to a circular import).
      

      Crucially, for folks encountering the error for the first time, that also introduces them to the main phrase they may want to search for: "circular import".

      The "most likely" weasel wording stems from the fact that the problem won't always be with the circular import - they may have just straight up referenced the wrong module or the wrong attribute name, so the apparently circular import is an error.

    4. brettcannon commented on Apr 7, 2018

      @brettcannon
      brettcannon
      on Apr 7, 2018
      Member
      More actions

      +1 from me for Nick's suggestion.

    5. ncoghlan commented on Apr 8, 2018

      @ncoghlan
      ncoghlan
      on Apr 8, 2018
      Contributor
      More actions

      Oops, just realised my suggested text had an extraneous double quote in it due to a copy-and-paste error. Fixed version:

      AttributeError: partially initialized module 'spam' has no attribute 'ham' (most likely due to a circular import).
      
    6. serhiy-storchaka commented on Apr 9, 2018

      @serhiy-storchaka
      serhiy-storchaka
      on Apr 9, 2018
      MemberAuthor
      More actions

      I have applied the Nick's suggestion. Needed to find a place for new test.

      The code is copied from PyImport_ImportModuleLevelObject(). I'm not happy from how verbose it is. And testing mod.__spec__._initialized adds relatively large overhead for importing already imported module in PyImport_ImportModuleLevelObject(). Is it possible to invent a faster way for checking whether the module is partially imported?

    7. ncoghlan commented on Apr 10, 2018

      @ncoghlan
      ncoghlan
      on Apr 10, 2018
      Contributor
      More actions

      The main idea that comes to mind is to cache a reference to _frozen_importlib._module_locks in the interpreter state, and do a key lookup in there (since any in-progress import should have a lock allocated to it).

      That would be a separate performance issue though - for this issue, we're on an error handling path, so the speed with which the error gets reported isn't critical (although it does technically slow down try/except import fallback chains).

    8. serhiy-storchaka commented on Oct 20, 2018

      @serhiy-storchaka
      serhiy-storchaka
      on Oct 20, 2018
      MemberAuthor
      More actions

      Example:

      $ cat foo.py
      import bar
      bar.baz
      $ cat bar.py
      import foo
      baz = 2
      $ ./python foo.py 
      Traceback (most recent call last):
        File "foo.py", line 1, in <module>
          import bar
        File "/home/serhiy/py/cpython/bar.py", line 1, in <module>
          import foo
        File "/home/serhiy/py/cpython/foo.py", line 2, in <module>
          bar.baz
      AttributeError: module 'bar' has no attribute 'baz'

      Patched:

      $ ./python foo.py 
      Traceback (most recent call last):
        File "foo.py", line 1, in <module>
          import bar
        File "/home/serhiy/py/cpython/bar.py", line 1, in <module>
          import foo
        File "/home/serhiy/py/cpython/foo.py", line 2, in <module>
          bar.baz
      AttributeError: partially initialized module 'bar' has no attribute 'baz' (most likely due to a circular import)
    9. serhiy-storchaka commented on Oct 30, 2018

      @serhiy-storchaka
      serhiy-storchaka
      on Oct 30, 2018
      MemberAuthor
      More actions

      New changeset 3e429dc by Serhiy Storchaka in branch 'master':
      bpo-33237: Improve AttributeError message for partially initialized module. (GH-6398)
      3e429dc

    10. serhiy-storchaka
      closed this as completedon Oct 30, 2018
    11. ezio-melotti
      transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.8 (EOL)end of lifeend of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions

        You are visitor 01402520 — GeezerVison does not store the pages you look up.
        Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.