]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
improve annotations for methods returning copies 1880/head
authorVictor Westerhuis <victor@westerhu.is>
Wed, 23 Aug 2023 07:01:55 +0000 (09:01 +0200)
committerDavid Lord <davidism@gmail.com>
Fri, 20 Dec 2024 04:26:44 +0000 (20:26 -0800)
CHANGES.rst
src/jinja2/compiler.py
src/jinja2/environment.py
src/jinja2/ext.py
src/jinja2/idtracking.py
src/jinja2/utils.py

index 2e83ab3f6092583f8d23327401d0f496fdba2a44..3955a32baf97f471784a47b762738e93f6d744c0 100644 (file)
@@ -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
index 23295ec1fdb2902b749ed75ce12b014ad8a99a53..ca079070a177ea4f27e1e218524cc72a07ef4e1d 100644 (file)
@@ -216,7 +216,7 @@ class Frame:
         # 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__)
@@ -229,7 +229,7 @@ class 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.
index 821971779660e24001c5997236368fd878512f7a..0fc6e5be87ab8273f6056ddfede07e1be28f1495 100644 (file)
@@ -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
@@ -407,7 +407,7 @@ class Environment:
         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
index 8d0810cd48025ec0ab46ae7ac4e930bf2731f7fe..c7af8d45f06ad77832d09d32468d283b5cda2912 100644 (file)
@@ -89,7 +89,7 @@ class Extension:
     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__)
index d6cb635b24a492581c32987ed3b8e5069c573ca7..cb4bccb0ed5d7faf0d8ea691cd3cc5bbfb5b56f1 100644 (file)
@@ -3,6 +3,9 @@ import typing as t
 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"
@@ -83,7 +86,7 @@ class Symbols:
             )
         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()
index 7b52fc03e08df61ce55246fb8121ef1b182ee5a1..d7149bc319fbc69896172ef3ffabf8c8bf20f071 100644 (file)
@@ -462,7 +462,7 @@ class LRUCache:
     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)