| 378 | |
| 379 | |
| 380 | class AttrCompletion(BaseCompletionType): |
| 381 | attr_matches_re = LazyReCompile(r"(\w+(\.\w+)*)\.(\w*)") |
| 382 | |
| 383 | def matches( |
| 384 | self, |
| 385 | cursor_offset: int, |
| 386 | line: str, |
| 387 | *, |
| 388 | locals_: dict[str, Any] | None = None, |
| 389 | **kwargs: Any, |
| 390 | ) -> set[str] | None: |
| 391 | r = self.locate(cursor_offset, line) |
| 392 | if r is None: |
| 393 | return None |
| 394 | |
| 395 | if locals_ is None: # TODO add a note about why |
| 396 | locals_ = __main__.__dict__ |
| 397 | |
| 398 | assert "." in r.word |
| 399 | |
| 400 | i = r.word.rfind("[") + 1 |
| 401 | methodtext = r.word[i:] |
| 402 | matches = { |
| 403 | "".join([r.word[:i], m]) |
| 404 | for m in self.attr_matches(methodtext, locals_) |
| 405 | } |
| 406 | |
| 407 | return { |
| 408 | m |
| 409 | for m in matches |
| 410 | if _few_enough_underscores(r.word.split(".")[-1], m.split(".")[-1]) |
| 411 | } |
| 412 | |
| 413 | def locate(self, cursor_offset: int, line: str) -> LinePart | None: |
| 414 | return lineparts.current_dotted_attribute(cursor_offset, line) |
| 415 | |
| 416 | def format(self, word: str) -> str: |
| 417 | return _after_last_dot(word) |
| 418 | |
| 419 | def attr_matches( |
| 420 | self, text: str, namespace: dict[str, Any] |
| 421 | ) -> Iterator[str]: |
| 422 | """Taken from rlcompleter.py and bent to my will.""" |
| 423 | |
| 424 | m = self.attr_matches_re.match(text) |
| 425 | if not m: |
| 426 | return (_ for _ in ()) |
| 427 | |
| 428 | expr, attr = m.group(1, 3) |
| 429 | if expr.isdigit(): |
| 430 | # Special case: float literal, using attrs here will result in |
| 431 | # a SyntaxError |
| 432 | return (_ for _ in ()) |
| 433 | try: |
| 434 | obj = safe_eval(expr, namespace) |
| 435 | except EvaluationError: |
| 436 | return (_ for _ in ()) |
| 437 | return self.attr_lookup(obj, expr, attr) |
no test coverage detected