1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
"""Module docstring."""
import sys
from contextlib import contextmanager
from typing import (
TYPE_CHECKING,
Any,
AsyncGenerator,
Callable,
Dict,
Generator,
List,
Literal,
Sequence,
Tuple,
Type,
TypeVar,
Union,
no_type_check,
no_type_check_decorator,
overload,
)
from typeguard import (
CollectionCheckStrategy,
ForwardRefPolicy,
typechecked,
typeguard_ignore,
)
if sys.version_info >= (3, 10):
from typing import ParamSpec
else:
from typing_extensions import ParamSpec
if TYPE_CHECKING:
from nonexistent import Imaginary
T = TypeVar("T", bound="DummyClass")
P = ParamSpec("P")
if sys.version_info <= (3, 13):
@no_type_check_decorator
def dummy_decorator(func):
return func
@dummy_decorator
def non_type_checked_decorated_func(x: int, y: str) -> 6:
# This is to ensure that we avoid using a local variable that's already in use
_call_memo = "foo" # noqa: F841
return "foo"
@typechecked
def type_checked_func(x: int, y: int) -> int:
return x * y
@no_type_check
def non_type_checked_func(x: int, y: str) -> 6:
return "foo"
@typeguard_ignore
def non_typeguard_checked_func(x: int, y: str) -> 6:
return "foo"
class Metaclass(type):
pass
@typechecked
class DummyClass(metaclass=Metaclass):
bar: str
baz: int
def type_checked_method(self, x: int, y: int) -> int:
return x * y
@classmethod
def type_checked_classmethod(cls, x: int, y: int) -> int:
return x * y
@staticmethod
def type_checked_staticmethod(x: int, y: int) -> int:
return x * y
@classmethod
def undocumented_classmethod(cls, x, y):
pass
@staticmethod
def undocumented_staticmethod(x, y):
pass
@property
def unannotated_property(self):
return None
def outer():
@typechecked
class Inner:
def get_self(self) -> "Inner":
return self
def create_inner() -> "Inner":
return Inner()
return create_inner
@typechecked
class Outer:
class Inner:
pass
def create_inner(self) -> "Inner":
return Outer.Inner()
@classmethod
def create_inner_classmethod(cls) -> "Inner":
return Outer.Inner()
@staticmethod
def create_inner_staticmethod() -> "Inner":
return Outer.Inner()
@contextmanager
@typechecked
def dummy_context_manager() -> Generator[int, None, None]:
yield 1
@overload
def overloaded_func(a: int) -> int: ...
@overload
def overloaded_func(a: str) -> str: ...
@typechecked
def overloaded_func(a: Union[str, int]) -> Union[str, int]:
return a
@typechecked
def missing_return() -> int:
pass
def get_inner_class() -> type:
@typechecked
class InnerClass:
def get_self(self) -> "InnerClass":
return self
return InnerClass
def create_local_class_instance() -> object:
class Inner:
pass
@typechecked
def get_instance() -> "Inner":
return instance
instance = Inner()
return get_instance()
@typechecked
async def async_func(a: int) -> str:
return str(a)
@typechecked
def generator_func(yield_value: Any, return_value: Any) -> Generator[int, Any, str]:
yield yield_value
return return_value
@typechecked
async def asyncgen_func(yield_value: Any) -> AsyncGenerator[int, Any]:
yield yield_value
@typechecked
def pep_604_union_args(
x: "Callable[[], Literal[-1]] | Callable[..., Union[int, str]]",
) -> None:
pass
@typechecked
def pep_604_union_retval(x: Any) -> "str | int":
return x
@typechecked
def builtin_generic_collections(x: "list[set[int]]") -> Any:
return x
@typechecked
def paramspec_function(func: P, args: P.args, kwargs: P.kwargs) -> None:
pass
@typechecked
def aug_assign() -> int:
x: int = 1
x += 1
return x
@typechecked
def multi_assign_single_value() -> Tuple[int, float, complex]:
x: int
y: float
z: complex
x = y = z = 6
return x, y, z
@typechecked
def multi_assign_iterable() -> Tuple[Sequence[int], Sequence[float], Sequence[complex]]:
x: Sequence[int]
y: Sequence[float]
z: Sequence[complex]
x = y = z = [6, 7]
return x, y, z
@typechecked
def unpacking_assign() -> Tuple[int, str]:
x: int
x, y = (1, "foo")
return x, y
@typechecked
def unpacking_assign_generator() -> Tuple[int, str]:
def genfunc():
yield 1
yield "foo"
x: int
x, y = genfunc()
return x, y
@typechecked
def unpacking_assign_star_with_annotation() -> Tuple[int, List[bytes], str]:
x: int
z: str
x, *y, z = (1, b"abc", b"bah", "foo")
return x, y, z
@typechecked
def unpacking_assign_star_no_annotation(value: Any) -> Tuple[int, List[bytes], str]:
x: int
y: List[bytes]
z: str
x, *y, z = value
return x, y, z
@typechecked
def attribute_assign_unpacking(obj: DummyClass) -> None:
obj.bar, obj.baz = "foo", 123123
@typechecked(forward_ref_policy=ForwardRefPolicy.ERROR)
def override_forward_ref_policy(value: "NonexistentType") -> None: # noqa: F821
pass
@typechecked(typecheck_fail_callback=lambda exc, memo: print(exc))
def override_typecheck_fail_callback(value: int) -> None:
pass
@typechecked(collection_check_strategy=CollectionCheckStrategy.ALL_ITEMS)
def override_collection_check_strategy(value: List[int]) -> None:
pass
@typechecked(typecheck_fail_callback=lambda exc, memo: print(exc))
class OverrideClass:
def override_typecheck_fail_callback(self, value: int) -> None:
pass
class Inner:
@typechecked
def override_typecheck_fail_callback(self, value: int) -> None:
pass
@typechecked
def typed_variable_args(
*args: str, **kwargs: int
) -> Tuple[Tuple[str, ...], Dict[str, int]]:
return args, kwargs
@typechecked
def guarded_type_hint_plain(x: "Imaginary") -> "Imaginary":
y: Imaginary = x
return y
@typechecked
def guarded_type_hint_subscript_toplevel(x: "Imaginary[int]") -> "Imaginary[int]":
y: Imaginary[int] = x
return y
@typechecked
def guarded_type_hint_subscript_nested(
x: List["Imaginary[int]"],
) -> List["Imaginary[int]"]:
y: List[Imaginary[int]] = x
return y
@typechecked
def literal(x: Literal["foo"]) -> Literal["foo"]:
y: Literal["foo"] = x
return y
@typechecked
def literal_in_union(x: Union[Literal["foo"],]) -> Literal["foo"]:
y: Literal["foo"] = x
return y
@typechecked
def typevar_forwardref(x: Type[T]) -> T:
return x()
def never_called(x: List["NonExistentType"]) -> List["NonExistentType"]: # noqa: F821
"""Regression test for #335."""
return x
|