Repository: python/mypy

Issue:
mypy does not recognize __qualname__
1. Given Python file `test1.py`
```Python
class A(object):
    NAME = __qualname__
```
2. run mypy checking

       mypy test1.py

3. **_BUG:_** mypy reports `not defined`

       test1.py:2: error: Name '__qualname__' is not defined

The Python interpreter runs the code without error.

`__qualname__` has [been available since Python 3.3](https://docs.python.org/3/library/stdtypes.html#definition.__qualname__).

Tested on _Python 3.7.1_, using _mpypy 0.670_.


Discussion:
Such kind of issues are typically easy to fix. You can look at how `implicit_module_attrs()` are implemented, and do the same for classes. Note however that we are currently migrating to a new semantic analyzer, so the fix should apply to both.
Thanks @ilevkivskyi .  I'm curious, why was this Issue labeled "false-positive"?
The label "false-positive" refers to the fact that mypy gives an error where it shouldn't. It doesn't mean there's anything wrong with your bug report!
If no one is working on this issue, I'd like to give it a shot if possible. :)
Go ahead! But please be aware that we are currently working on big refactoring on semantic analyzer, so you will need to copy your fix to the `newsemanal` as well.
So I believe I fixed the issue within the normal mypy directory, but I'm not sure where in newsemanal it would be necessary to make a change? The only place I can think to make additions or changes would be in newsemanal/semanal_pass1.py, otherwise it doesn't look like there would be a change needed other than the one in mypy/mypy/nodes.py where ```implicit_module_attrs()``` is
I don't think using/changing `implicit_module_attrs` is the best solution. `__qualname__` is only defined at class scope, at module scope it is a `NameError`. Probably we should have something like `implicit_class_attrs`.
Any tips on implementing that? implicit_module_attrs uses a set with 'module name attributes' keys and 'type' values so for an implicit_class_attrs would there be even be a 'type'? I'm just wondering if a set would even be the correct solution. Also, what other classes, if any, would you recommend I add?
@aaalex5 I started a pull request https://github.com/python/mypy/pull/7683.
#10570 is a duplicate with slightly more details

Patch:
diff --git a/mypy/semanal.py b/mypy/semanal.py
--- a/mypy/semanal.py
+++ b/mypy/semanal.py
@@ -5569,7 +5569,7 @@ def lookup(
             if not suppress_errors:
                 self.name_not_defined(name, ctx)
             return None
-        # 2. Class attributes (if within class definition)
+        # 2a. Class attributes (if within class definition)
         if self.type and not self.is_func_scope() and name in self.type.names:
             node = self.type.names[name]
             if not node.implicit:
@@ -5579,6 +5579,9 @@ def lookup(
                 # Defined through self.x assignment
                 implicit_name = True
                 implicit_node = node
+        # 2b. Class attributes __qualname__ and __module__
+        if self.type and not self.is_func_scope() and name in {"__qualname__", "__module__"}:
+            return SymbolTableNode(MDEF, Var(name, self.str_type()))
         # 3. Local (function) scopes
         for table in reversed(self.locals):
             if table is not None and name in table:


Test patch:
diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test
--- a/test-data/unit/check-classes.test
+++ b/test-data/unit/check-classes.test
@@ -8001,3 +8001,11 @@ f5(1)  # E: Argument 1 to "f5" has incompatible type "int"; expected "Integral"
        # N: Types from "numbers" aren't supported for static type checking \
        # N: See https://peps.python.org/pep-0484/#the-numeric-tower \
        # N: Consider using a protocol instead, such as typing.SupportsFloat
+
+[case testImplicitClassScopedNames]
+class C:
+    reveal_type(__module__)  # N: Revealed type is "builtins.str"
+    reveal_type(__qualname__)  # N: Revealed type is "builtins.str"
+    def f(self) -> None:
+        __module__  # E: Name "__module__" is not defined
+        __qualname__  # E: Name "__qualname__" is not defined