Skip to content

Commit

Permalink
improve annotations for methods returning copies (#1880)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism authored Dec 20, 2024
2 parents 53c7591 + ded9915 commit 106d61c
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Unreleased
searched. :issue:`1661`
- ``PackageLoader`` shows a clearer error message when the package does not
contain the templates directory. :issue:`1705`
- Improve annotations for methods returning copies. :pr:`1880`


Version 3.1.4
Expand Down
4 changes: 2 additions & 2 deletions src/jinja2/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def __init__(
# or compile time.
self.soft_frame = False

def copy(self) -> "Frame":
def copy(self) -> "te.Self":
"""Create a copy of the current one."""
rv = object.__new__(self.__class__)
rv.__dict__.update(self.__dict__)
Expand All @@ -229,7 +229,7 @@ def inner(self, isolated: bool = False) -> "Frame":
return Frame(self.eval_ctx, level=self.symbols.level + 1)
return Frame(self.eval_ctx, self)

def soft(self) -> "Frame":
def soft(self) -> "te.Self":
"""Return a soft frame. A soft frame may not be modified as
standalone thing as it shares the resources with the frame it
was created of, but it's not a rootlevel frame any longer.
Expand Down
4 changes: 2 additions & 2 deletions src/jinja2/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def load_extensions(
return result


def _environment_config_check(environment: "Environment") -> "Environment":
def _environment_config_check(environment: _env_bound) -> _env_bound:
"""Perform a sanity check on the environment."""
assert issubclass(
environment.undefined, Undefined
Expand Down Expand Up @@ -407,7 +407,7 @@ def overlay(
auto_reload: bool = missing,
bytecode_cache: t.Optional["BytecodeCache"] = missing,
enable_async: bool = missing,
) -> "Environment":
) -> "te.Self":
"""Create a new overlay environment that shares all the data with the
current environment except for cache and the overridden attributes.
Extensions cannot be removed for an overlayed environment. An overlayed
Expand Down
2 changes: 1 addition & 1 deletion src/jinja2/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init_subclass__(cls) -> None:
def __init__(self, environment: Environment) -> None:
self.environment = environment

def bind(self, environment: Environment) -> "Extension":
def bind(self, environment: Environment) -> "te.Self":
"""Create a copy of this extension bound to another environment."""
rv = object.__new__(self.__class__)
rv.__dict__.update(self.__dict__)
Expand Down
5 changes: 4 additions & 1 deletion src/jinja2/idtracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from . import nodes
from .visitor import NodeVisitor

if t.TYPE_CHECKING:
import typing_extensions as te

VAR_LOAD_PARAMETER = "param"
VAR_LOAD_RESOLVE = "resolve"
VAR_LOAD_ALIAS = "alias"
Expand Down Expand Up @@ -83,7 +86,7 @@ def ref(self, name: str) -> str:
)
return rv

def copy(self) -> "Symbols":
def copy(self) -> "te.Self":
rv = object.__new__(self.__class__)
rv.__dict__.update(self.__dict__)
rv.refs = self.refs.copy()
Expand Down
2 changes: 1 addition & 1 deletion src/jinja2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def __setstate__(self, d: t.Mapping[str, t.Any]) -> None:
def __getnewargs__(self) -> t.Tuple[t.Any, ...]:
return (self.capacity,)

def copy(self) -> "LRUCache":
def copy(self) -> "te.Self":
"""Return a shallow copy of the instance."""
rv = self.__class__(self.capacity)
rv._mapping.update(self._mapping)
Expand Down

0 comments on commit 106d61c

Please sign in to comment.