pola-rs/polars · error · AttributeError

' ' object has no attribute

Error message

'{type(self).__name__}' object has no attribute {name!r}

What it means

This AttributeError is raised by `_NamespaceSuggestMixin.__getattr__`, a mixin installed on Polars namespaces, when accessing an attribute that does not exist. If difflib finds a close match among the public methods, the message appends 'Did you mean: ...' to suggest the intended name. It replaces the default AttributeError to give typo-friendly diagnostics.

Solutions

  1. Read the 'Did you mean' hint in the message and use the suggested attribute
  2. Run `dir(obj)` or check the Polars API docs for the correct method name
  3. Pin/align your code with the Polars version's API reference when upgrading

Example fix

// before
df.select(pl.col("a").str.tolower())  # AttributeError, Did you mean: 'to_lowercase'?
// after
df.select(pl.col("a").str.to_lowercase())
Defensive patterns

Strategy: type-guard

Validate before calling

if not hasattr(ns, "to_lowercase"):
    print([m for m in dir(ns) if not m.startswith('_')])

Type guard

def has_ns_method(ns: object, name: str) -> bool:
    return not name.startswith('_') and callable(getattr(type(ns), name, None))

Try / catch

try:
    out = df.select(pl.col('a').str.tolower())
except AttributeError as e:
    if 'object has no attribute' in str(e):
        logging.warning("namespace typo: %s", e)  # message includes 'Did you mean'
    else:
        raise

Prevention

When it happens

Trigger: Typing a misspelled namespace method, e.g. `df.str.toupper()` instead of `to_uppercase`, or `s.dt.days_in_month()` instead of `month_start`-style names, on any str/dt/etc. namespace object.

Common situations: Upgrading Polars after a method rename; mixing up camelCase/snake_case conventions from other libraries (pandas, spark); autocomplete not used in notebooks.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/42ef84035c53abfe. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/_utils/various.py:756

            f"expected `other` to be a {qualified_type_name(current)!r}, "
            f"not {qualified_type_name(other)!r}"
        )
        raise TypeError(msg)


class _NamespaceSuggestMixin:
    """Mixin that adds suggestions to AttributeError on namespace typos."""

    def __getattr__(self, name: str) -> NoReturn:
        import difflib

        public = [m for m in dir(type(self)) if not m.startswith("_")]
        matches = difflib.get_close_matches(name, public, n=1, cutoff=0.6)
        if matches:
            msg = f"'{type(self).__name__}' object has no attribute {name!r}. Did you mean: {matches[0]!r}?"
        else:
            msg = f"'{type(self).__name__}' object has no attribute {name!r}"
        raise AttributeError(msg)

View on GitHub (pinned to fe841f959e)