Textualize/textual · error · NoMatches

No immediate child of type {expect_type}; {self._nodes}

Error message

No immediate child of type {expect_type}; {self._nodes}

What it means

get_child_by_type requires an immediate child whose exact type (type(child) is expect_type, not a subclass) matches; NoMatches lists the actual children for debugging when none matches.

Source

Thrown at src/textual/widget.py:1173

        """Get the first immediate child of a given type.

        Only returns exact matches, and so will not match subclasses of the given type.

        Args:
            expect_type: The type of the child to search for.

        Raises:
            NoMatches: If no matching child is found.

        Returns:
            The first immediate child widget with the expected type.
        """
        for child in self._nodes:
            # We want the child with the exact type (not subclasses)
            if type(child) is expect_type:
                assert isinstance(child, expect_type)
                return child
        raise NoMatches(f"No immediate child of type {expect_type}; {self._nodes}")

    def get_component_rich_style(
        self, *names: str, partial: bool = False, default: Style | None = None
    ) -> Style:
        """Get a *Rich* style for a component.

        Args:
            names: Names of components.
            partial: Return a partial style (not combined with parent).
            default: A Style to return if any component style doesn't exist.

        Raises:
            KeyError: If a component style doesn't exist, and no `default` is provided.

        Returns:
            A Rich style object.
        """

View on GitHub (pinned to 06dbeef4bb)

Solutions

  1. Use query_one with a type selector, which matches subclasses: query_one(ListView)
  2. Ensure the child is created in compose() as a direct child of this widget
  3. If mounted dynamically, wait for mount (call_after_refresh) before lookup

Example fix

# before
child = self.get_child_by_type(MyListView)
# after
child = self.query_one(MyListView)
Defensive patterns

Strategy: type-guard

Validate before calling

if any(type(child) is expect_type for child in self._nodes):
    child = self.get_child_by_type(expect_type)

Type guard

def has_exact_child_type(widget: Widget, t: type) -> bool:
    return any(type(child) is t for child in widget._nodes)

Try / catch

try:
    child = self.get_child_by_type(T)
except NoMatches:
    child = self.query_one(T)  # subclass-tolerant fallback

Prevention

When it happens

Trigger: Calling get_child_by_type(ListView) when the child is a subclass of ListView, a different type, or the child lives deeper than one level.

Common situations: Custom widgets wrapping stock widgets (subclass no longer matches), or children added later via mount() so they aren't present yet.

Related errors


AI-assisted analysis of Textualize/textual@06dbeef4bb (2026-08-27). Data as JSON: /api/errors/7406374cef7d0c9e. Report an issue: GitHub.