This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Just defined class missing from scope
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: rhettinger, yurzo
Priority: normal Keywords:

Created on 2020-03-21 16:49 by yurzo, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg364759 - (view) Author: Damian Yurzola (yurzo) Date: 2020-03-21 16:49
In the following example the last line throws as 'NameError: name 'Level1A' is not defined' for both 3.7 and 3.8

I assumed that Level1A should already be in scope while defining the insides of Level1B. But it isn't.
Is this a bug, or am I missing something?


from typing import List, Union


class Level0A:
    pass


class Level0B:
    class Level1A:
        subs: List[Level0A]

    class Level1B:
        subs: List[Level1A]
msg364761 - (view) Author: Damian Yurzola (yurzo) Date: 2020-03-21 16:58
This is even a better example:

Level1A is available to inherit from, but not to type with.

Example:

from typing import List


class Level0A:
    pass


class Level0B:
    class Level1A:
        pass

    class Level1B(Level1A):
        pass

    class Level1C:
        test: Level1A
msg364874 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-03-23 18:21
> I assumed that Level1A should already be in scope while defining 
> the insides of Level1B. But it isn't.

That analysis is correct.  Class namespaces don't nest like functions do.  The "enclosing" namespace is globals, not the surrounding class.

This isn't a bug, but it is understandable why you might assume otherwise.
History
Date User Action Args
2022-04-11 14:59:28adminsetgithub: 84214
2020-03-23 18:21:56rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg364874

resolution: not a bug
stage: resolved
2020-03-23 15:48:10yurzosetversions: + Python 3.7
2020-03-21 16:58:12yurzosetmessages: + msg364761
2020-03-21 16:49:09yurzocreate