Skip to content

Resolve inherited class attributes along the C3 method resolution order - #920

Closed
marlon-costa-dc wants to merge 328 commits into
python-rope:masterfrom
marlon-costa-dc:pr/c3-mro
Closed

marlon-costa-dc wants to merge 328 commits into
python-rope:masterfrom
marlon-costa-dc:pr/c3-mro

Conversation

@marlon-costa-dc

Copy link
Copy Markdown
Contributor

Class attributes are inherited by a depth-first walk of the bases, so in a diamond a class can inherit the member of a base that Python's __mro__ places later:

class A:
    def method(self): ...
class B(A): pass
class C(A):
    def method(self): ...
class D(B, C): pass   # Python: D.method is C.method; rope: A.method

The same walk picks inherited docstring and assignment type hints, so a parameter hint can come from the wrong base.

This linearizes the bases with C3, as Python does, and takes each attribute from the first ancestor in that order that defines it. Type hint inheritance follows the same order.

  • AbstractClass.get_mro() returns the linearization, starting with the class. It raises a new InconsistentHierarchyError for cyclic, repeated and unmergeable bases, which Python refuses to create.
  • AbstractClass.get_consistent_mro() returns None for those classes instead of raising. Rope keeps analysing them along their bases as it does today, so attributes, completions and refactorings on such code are unchanged and nothing new raises in existing APIs.

Tests cover nested hierarchies against Python's __mro__, diamond attributes, docstring hints along the MRO, and inconsistent, duplicate, cyclic and inherited-inconsistent hierarchies. The diamond-attribute and hint tests fail on master. Tested locally on Python 3.10, 3.11, 3.13 and 3.14.

# Conflicts:
#	CHANGELOG.md
# Conflicts:
#	CHANGELOG.md
# Conflicts:
#	CHANGELOG.md
#	rope/refactor/inline.py
# Conflicts:
#	CHANGELOG.md
# Conflicts:
#	CHANGELOG.md
# Conflicts:
#	CHANGELOG.md
#	rope/base/evaluate.py
#	rope/refactor/introduce_parameter.py
# Conflicts:
#	CHANGELOG.md
#	rope/base/evaluate.py
#	rope/refactor/rename.py
# Conflicts:
#	CHANGELOG.md
#	rope/refactor/rename.py
# Conflicts:
#	CHANGELOG.md
#	rope/refactor/inline.py
# Conflicts:
#	CHANGELOG.md
#	rope/base/evaluate.py
# Conflicts:
#	CHANGELOG.md
…dast

_arguments walked only args, vararg and kwarg, and _arg consumed only the
parameter name. Positional-only separators, bare '*', keyword-only
parameters and annotations were skipped by the forward token search, so
`def f(a, /, b, *, c)` produced a single child and annotations were
swallowed as whitespace; a second annotation containing a call could
raise MismatchedTokenError.

_arguments now emits every signature token in source order and _arg
walks its annotation. Unannotated *args/**kwargs keep their name-string
children, so existing region expectations are unchanged.
The requires-python floor is 3.10, so ast.PyCF_ALLOW_TOP_LEVEL_AWAIT
always exists and _AsyncStatementFinder can never run.
The regex finder captured f-strings and parsed each capture with
ast.parse to locate names inside replacement fields; a PEP 701
f-string whose replacement field reuses the outer quote or carries a
format spec produces a capture that is not valid standalone source, so
rename missed occurrences there (and issue python-rope#815 reports ast.parse
SyntaxErrors). Tokenizing the source instead yields every NAME token,
including those inside 3.12+ replacement fields, and never matches
names inside comments or string literals.
…e pynames

The inheritance provider hands DefinedName pynames (import rebinds) to
the type-comment provider, which unconditionally reads pyname.assignments
and raises AttributeError; return None so inference falls through to the
next composite provider.
end_quote_char() searched the whole literal for the longest quote
sequence, so a double-quoted f-string containing a triple-quote
sequence returned a suffix of the body as its own delimiter. Read
backward from the region end instead, and raise
MismatchedTokenError when no quote terminates it.
Scope.get_region() no longer patches the whole module AST through
patchedast to borrow node regions.  It now resolves each scope's
region directly from the native AST positions via
codeanalyze.ASTLinesAdapter, and extends it to the enclosing
decorator by searching backwards for the "@" token with
_Source.rfind_token.

This keeps regions correct for sources the patched walker cannot
round-trip: normalized Unicode identifiers (e.g. "u\u0301" aliases),
decorated definitions, and raw string sources using LF, CRLF, or CR
newlines.  Modules resolve to the full source range, and nodes
without complete end positions raise RopeError instead of silently
producing partial regions.

Note: the original internal change also fixed _Source tokenization
for CR/CRLF sources; that code path was removed by the patchedast
cleanup (python-rope#896), which already computes positions through
ASTLinesAdapter, so only the pyscopes resolution is carried over.
_get_surrounding_parens stripped the text around the first and last
patterns and emitted a please-report RuntimeWarning whenever that
text was not a single bracket, which happens for grouping parentheses
of an element (case (("a" | b), c)) and for sequences whose elements
are separately parenthesized (case (1), (2)). Tokenize the segment
instead: the sequence has its own delimiters exactly when the bracket
opening its first character closes at its last character.
Three combinations not exercised by the existing parametrizations:
a signature with positional-only plus keyword-only but no *args, a
method whose only parameter is keyword-only (the receiver must not
be injected), and saved object analysis for a call mixing positional,
*args, and keyword-only arguments.
Project.find_module() resolved each dotted component inside a single
folder at a time, so a namespace-package directory (a folder without
__init__.py) shadowed a regular module or package living in a later
source folder, and namespace portions were never combined.

Resolve each name component across all candidate folders instead:
regular packages and plain modules win over namespace directories
regardless of folder order, while namespace portions are collected
across folders so their submodules remain reachable.  Relative module
resolution keeps searching only the given folder.

Adapted to 1.15.0 from the internal change: only the resolution
semantics and its pycore tests are carried over.
The function walker stopped at the closing parenthesis, so a return
annotation had no region of its own, and the class walker ignored
node.keywords, leaving metaclass and *bases/**options without regions
and mis-regioning interleaved headers. Walk -> returns after the
argument list and interleave keyword nodes with bases by position.
Manual workflow dispatch, pinned action SHAs, the Codecov OIDC/GPG
keyring authentication, coverage artifact naming, and the pip cache
key for the dev requirements.
….typed

Declare the license with the SPDX license/license-files keys, discover
packages instead of listing them, ship the py.typed marker, and widen
the coverage source set.
The dc-use merge combined python-rope#900's binding_scope fast path with python-rope#902's
_is_local body but dropped the isinstance(pyname, AssignedName) guard
that upstream 22f5e50 and python-rope#902 both keep in the fallback path.

Two interactions broke:
- ParameterName fell into the fallback and matched its own function
  scope, so parameter renames were restricted to the defining module
  and keyword arguments at cross-module call sites were left behind
  (renametest test_rename_functions_parameters_and_occurrences_in_other_modules,
  class_function_access_test test_imported_inherited_function_parameter_rename).
- ImportedModule pynames for folders (namespace packages) reached
  get_definition_location -> PyPackage.get_scope -> _create_scope ->
  get_module().get_scope() and recursed forever
  (namespace_package_rename_test, all 8 cases).
The or/| type-hint evaluator raised NotImplementedError, so every
completion on a union-annotated name lost both alternatives. Evaluate
to a synthesized class exposing the attributes of each alternative,
which also un-xfails the hint_or completion test.
FileSystemCommands gained the query ports exists, isfile, isdir,
listdir, stat, and realpath, and SubversionCommands, MercurialCommands,
GITCommands, and DarcsCommands now subclass it so every implementation
offers the full interface.

Project.get_resource(), Project.__init__ root election, Resource.exists(),
Folder.get_children(), and the resource observer ChangeIndicator no
longer call os.path/os.listdir/os.stat directly: a custom fscommands
object previously leaked those reads to the real filesystem.  Directory
enumeration and metadata errors now propagate to the caller instead of
being reported as an empty result.
…e check

python-rope#900's source binding index gives annotation expressions their own
annotation environment, so eval_node on an annotation node now resolves
shadowing class members instead of the module global.  The deferred
annotation check in InlineVariable._check_deferred_annotation_references
(inline_annotations_test, from the python-rope#894/python-rope#903 series) relied on
eval_node(pymodule.get_scope(), node) resolving module globals first,
and stopped refusing inlines that typing.get_type_hints would break at
runtime (AttributeError: module 'source' has no attribute 'target').

typing.get_type_hints evaluates class annotations with module globals
ahead of class/function locals (it swaps the namespaces for classes).
Implement that order in the check: module-level bindings win for name
nodes; only names without a module-level binding fall back to the
annotation environment resolution; attribute chains evaluate their
receiver with the same rule.
marlon-costa-dc and others added 24 commits November 13, 2026 11:48
`X: TypeAlias = Y` declares an alias, not an instance of the annotation, so it
is now inferred like `X = Y`. The alias is recognised by semantics: the
annotation must resolve to the runtime `typing.TypeAlias` form (directly, via
`typing.TypeAlias`, a renamed import, or the `typing_extensions` re-export),
never by its spelling — a user class named `TypeAlias` still types an instance.

Classes based on such aliases (including generic subscripts and facade class
attributes) now resolve their superclass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`_evaluate_assign_value` defaulted `type_hint` to False, so assignments built
without an annotation carried a non-AST hint that the TypeAlias check evaluated.
The default now follows `AssignmentValue`'s own contract.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The fork is consumed only on Python 3.13 and newer. requires-python,
classifiers, the black target and the CI matrix now say so, so tests may use
PEP 695 syntax (e.g. `class Root[T]`) without a 3.10-3.12 job rejecting it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…inds-its-value-20261113

fix(soi): bind a PEP 613 TypeAlias assignment to its value
Integrate upstream master for Rope 1.15.2+dc.7
…ification-20261114

Qualify native nested moves and consumer replay
PEP 484 has static analysis assume typing.TYPE_CHECKING is true. Branch
sensitive inference explored both arms of such a guard; a name assigned
only in its body (a class-body TypeAlias re-export) disagreed with the
empty false arm and inferred as unknown, and class bases read through it
left get_superclasses() and the MRO.

When the read context selects no arm, _infer_assignment_path now takes
the body of a branch whose test resolves, through rope's own evaluation,
to the definition of typing.TYPE_CHECKING. Import aliases and re-exports
follow; an unrelated name spelled TYPE_CHECKING keeps both arms open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…inference-20261115

Take the typing.TYPE_CHECKING branch in assignment inference
Review of #52. The branch test is no longer evaluated, and typing is no
longer looked up:

- A test is followed through bindings only: a name, an attribute of an
  imported module, ImportedName targets and a name's single plain
  assignment, each binding at most once. No object is inferred, so a test
  such as `a.flag` read while `a` is being inferred no longer raises
  IsBeingInferredError into the inference of `a` and discards its
  agreeing arms.
- The test reads typing.TYPE_CHECKING when it ends at the binding a
  module named `typing` holds under that name. A project `typing.py`
  without the constant, or a missing module, reads as not TYPE_CHECKING
  instead of raising AttributeNotFoundError or ModuleNotFoundError.
- typing_extensions' `TYPE_CHECKING = typing.TYPE_CHECKING` is followed
  through that single assignment. Limits: a name with more than one
  assignment, tuple targets, module aliases bound by assignment
  (`t = typing; t.TYPE_CHECKING`) and composite tests such as
  `not TYPE_CHECKING` stay undecided.
- The decision is kept per test in the module's concluded data
  (_PyModule._type_checking_tests), which PyCore forgets with all other
  concluded data when a module changes. Typing is parsed only when a test
  imports from it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A name followed to typing.TYPE_CHECKING must be written exactly once.
Augmented assignments, deletions and assignments under global/nonlocal
are not in the name's assignments, so the source binding index now
reports every store and deletion that resolves to the binding.

State that the constant is recognized by its place in the module named
typing, not by its value, and test the cached decision's invalidation
when the binding module changes or a project typing module is removed.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…inference-20261115

Decide TYPE_CHECKING tests from bindings, once per module state
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants