Repository: pylint-dev/pylint

Issue:
# Wrong undefined-variable detection after object is used as metaclass

### Bug description

```python
from foo import bar


class Test:

    def test1(self):

        class A(metaclass=bar.Meta):
            pass


bar.Meta  # Triggers E0602: Undefined variable 'bar' (undefined-variable)
```

What seems to be happening: `bar` is passed as `metaclass` parameter of a class defined inside of a method of another class. This somehow makes it undefined. Because of that using `bar` anywhere else later in the module will trigger an `undefined-variable` error.

### Configuration

```ini

```

### Command used

```shell
pylint -E undefined_var.py
```

### Pylint output

```python
************* Module undefined_var
undefined_var.py:12:0: E0602: Undefined variable 'bar' (undefined-variable)
```

### Expected behavior

`bar` is a well defined object. `undefined-variable` shouldn't be raised.

### Pylint version

```shell
pylint 4.0.4
astroid 4.0.3
Python 3.13

(Also reproducible with earlier versions.)
```

### OS / Environment

Not important.

### Additional dependencies

```python

```

Implementation patch:
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
index 5add0732c0..fbb7f69072 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -3387,27 +3387,29 @@ def _check_imports(self, not_consumed: Consumption) -> None:
 
     def _check_metaclasses(self, node: nodes.Module | nodes.FunctionDef) -> None:
         """Update consumption analysis for metaclasses."""
-        consumed: list[tuple[Consumption, str]] = []
+        consumed: list[tuple[NamesConsumer, str, list[nodes.NodeNG]]] = []
 
         for child_node in node.get_children():
             if isinstance(child_node, nodes.ClassDef):
                 consumed.extend(self._check_classdef_metaclasses(child_node, node))
 
-        # Pop the consumed items, in order to avoid having
-        # unused-import and unused-variable false positives
-        for scope_locals, name in consumed:
-            scope_locals.pop(name, None)
+        # Mark the consumed items properly so they move from to_consume
+        # to consumed, avoiding unused-import/unused-variable false positives
+        # while still allowing subsequent references to resolve.
+        for consumer, name, found_nodes in consumed:
+            if name in consumer.to_consume:
+                consumer.mark_as_consumed(name, found_nodes)
 
     def _check_classdef_metaclasses(
         self,
         klass: nodes.ClassDef,
         parent_node: nodes.Module | nodes.FunctionDef,
-    ) -> list[tuple[Consumption, str]]:
+    ) -> list[tuple[NamesConsumer, str, list[nodes.NodeNG]]]:
         if not klass._metaclass:
             # Skip if this class doesn't use explicitly a metaclass, but inherits it from ancestors
             return []
 
-        consumed: list[tuple[Consumption, str]] = []
+        consumed: list[tuple[NamesConsumer, str, list[nodes.NodeNG]]] = []
         metaclass = klass.metaclass()
         name = ""
         match klass._metaclass:
@@ -3433,7 +3435,7 @@ def _check_classdef_metaclasses(
                 found_nodes = scope_locals.get(name, [])
                 for found_node in found_nodes:
                     if found_node.lineno <= klass.lineno:
-                        consumed.append((scope_locals, name))
+                        consumed.append((to_consume, name, found_nodes))
                         found = True
                         break
             # Check parent scope


Test patch:
diff --git a/tests/functional/u/undefined/undefined_variable_metaclass_nested.py b/tests/functional/u/undefined/undefined_variable_metaclass_nested.py
new file mode 100644
index 0000000000..b1a29d0c8e
--- /dev/null
+++ b/tests/functional/u/undefined/undefined_variable_metaclass_nested.py
@@ -0,0 +1,20 @@
+"""Tests for undefined-variable false positive when a name is used as a metaclass
+in a nested class inside a method, and then used again at module level.
+
+https://github.com/pylint-dev/pylint/issues/10823
+"""
+# pylint: disable=too-few-public-methods,missing-class-docstring,missing-function-docstring
+# pylint: disable=unused-variable,pointless-statement
+
+import abc
+
+
+class Test:
+    def test1(self):
+        class A(metaclass=abc.ABCMeta):
+            pass
+
+
+# This should NOT trigger undefined-variable — abc was imported at module level
+# and consumed by the metaclass usage, but it should still be resolvable.
+abc.ABCMeta