GitHub

API reference for Scope.

A declaration scope of one module (Module.scope).

Functions

__init__

__init__(owner=None, parent=None) -> None
ParameterTypeDescription
ownerobject | None = None
parentScope | None = None
def __init__(self, owner: object | None = None, parent: Scope | None = None) -> None:
    self._owner = object() if owner is None else owner
    self._parent = parent
    self._taken: set[str] = set()
    self._nested: set[str] = set()

nested

nested() -> Scope

Returns Scope

def nested(self) -> Scope:
    return Scope(self._owner, self)

in_module_of

in_module_of(other) -> bool
ParameterTypeDescription
otherScope

Returns bool

def in_module_of(self, other: Scope) -> bool:
    return self._owner is other._owner

reserve

reserve(name) -> None
ParameterTypeDescription
namestr
def reserve(self, name: str) -> None:
    self._taken.add(name)
    current = self._parent
    while current is not None:
        current._nested.add(name)
        current = current._parent

taken_by

taken_by(name) -> str | None

Why name cannot be declared here, or None when it can.

ParameterTypeDescription
namestr

Returns str | None

def taken_by(self, name: str) -> str | None:
    """Why ``name`` cannot be declared here, or ``None`` when it can."""
    if name in self._nested:
        return "a nested scope declares it"
    current: Scope | None = self
    while current is not None:
        if name in current._taken:
            return (
                "this scope declares it"
                if current is self
                else "an enclosing scope declares it"
            )
        current = current._parent
    return None

names

names() -> set[str]

Returns set[str]

def names(self) -> set[str]:
    return self._taken | self._nested