@schemastore/ty 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +14 -0
- package/index.d.ts +3278 -0
- package/package.json +10 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,3278 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
|
|
3
|
+
export type String = string;
|
|
4
|
+
/**
|
|
5
|
+
* A possibly relative path in a configuration file.
|
|
6
|
+
*
|
|
7
|
+
* Relative paths in configuration files or from CLI options
|
|
8
|
+
* require different anchoring:
|
|
9
|
+
*
|
|
10
|
+
* * CLI: The path is relative to the current working directory
|
|
11
|
+
* * Configuration file: The path is relative to the project's root.
|
|
12
|
+
*/
|
|
13
|
+
export type RelativePathBuf = SystemPathBuf;
|
|
14
|
+
/**
|
|
15
|
+
* An owned, mutable path on [`System`](`super::System`) (akin to [`String`]).
|
|
16
|
+
*
|
|
17
|
+
* The path is guaranteed to be valid UTF-8.
|
|
18
|
+
*/
|
|
19
|
+
export type SystemPathBuf = string;
|
|
20
|
+
/**
|
|
21
|
+
* The target platform to assume when resolving types.
|
|
22
|
+
*
|
|
23
|
+
*/
|
|
24
|
+
export type PythonPlatform = string | 'all' | 'darwin' | 'linux' | 'win32';
|
|
25
|
+
export type PythonVersion = string | '3.7' | '3.8' | '3.9' | '3.10' | '3.11' | '3.12' | '3.13' | '3.14' | '3.15';
|
|
26
|
+
export type ArrayOfString = String[];
|
|
27
|
+
/**
|
|
28
|
+
* ## What it does
|
|
29
|
+
* Checks for `@final` classes that have unimplemented abstract methods.
|
|
30
|
+
*
|
|
31
|
+
* ## Why is this bad?
|
|
32
|
+
* A class decorated with `@final` cannot be subclassed. If such a class has abstract
|
|
33
|
+
* methods that are not implemented, the class can never be properly instantiated, as
|
|
34
|
+
* the abstract methods can never be implemented (since subclassing is prohibited).
|
|
35
|
+
*
|
|
36
|
+
* At runtime, instantiation of classes with unimplemented abstract methods is only
|
|
37
|
+
* prevented for classes that have `ABCMeta` (or a subclass of it) as their metaclass.
|
|
38
|
+
* However, type checkers also enforce this for classes that do not use `ABCMeta`, since
|
|
39
|
+
* the intent for the class to be abstract is clear from the use of `@abstractmethod`.
|
|
40
|
+
*
|
|
41
|
+
* ## Example
|
|
42
|
+
*
|
|
43
|
+
* ```python
|
|
44
|
+
* from abc import ABC, abstractmethod
|
|
45
|
+
* from typing import final
|
|
46
|
+
*
|
|
47
|
+
* class Base(ABC):
|
|
48
|
+
* @abstractmethod
|
|
49
|
+
* def method(self) -> int: ...
|
|
50
|
+
*
|
|
51
|
+
* @final
|
|
52
|
+
* class Derived(Base): # Error: `Derived` does not implement `method`
|
|
53
|
+
* pass
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export type DetectsFinalClassesWithUnimplementedAbstractMethods = Level;
|
|
57
|
+
export type Level = Ignore | Warn | Error;
|
|
58
|
+
/**
|
|
59
|
+
* The lint is disabled and should not run.
|
|
60
|
+
*/
|
|
61
|
+
export type Ignore = 'ignore';
|
|
62
|
+
/**
|
|
63
|
+
* The lint is enabled and diagnostic should have a warning severity.
|
|
64
|
+
*/
|
|
65
|
+
export type Warn = 'warn';
|
|
66
|
+
/**
|
|
67
|
+
* The lint is enabled and diagnostics have an error severity.
|
|
68
|
+
*/
|
|
69
|
+
export type Error = 'error';
|
|
70
|
+
/**
|
|
71
|
+
* Configure a default severity level for all rules. Individual rule settings override this default.
|
|
72
|
+
*/
|
|
73
|
+
export type SetTheDefaultSeverityLevelForAllRules = Level;
|
|
74
|
+
/**
|
|
75
|
+
* ## What it does
|
|
76
|
+
* Checks for protocol classes with members that will lead to ambiguous interfaces.
|
|
77
|
+
*
|
|
78
|
+
* ## Why is this bad?
|
|
79
|
+
* Assigning to an undeclared variable in a protocol class leads to an ambiguous
|
|
80
|
+
* interface which may lead to the type checker inferring unexpected things. It's
|
|
81
|
+
* recommended to ensure that all members of a protocol class are explicitly declared.
|
|
82
|
+
*
|
|
83
|
+
* ## Examples
|
|
84
|
+
*
|
|
85
|
+
* ```py
|
|
86
|
+
* from typing import Protocol
|
|
87
|
+
*
|
|
88
|
+
* class BaseProto(Protocol):
|
|
89
|
+
* a: int # fine (explicitly declared as `int`)
|
|
90
|
+
* def method_member(self) -> int: ... # fine: a method definition using `def` is considered a declaration
|
|
91
|
+
* c = "some variable" # error: no explicit declaration, leading to ambiguity
|
|
92
|
+
* b = method_member # error: no explicit declaration, leading to ambiguity
|
|
93
|
+
*
|
|
94
|
+
* # error: this creates implicit assignments of `d` and `e` in the protocol class body.
|
|
95
|
+
* # Were they really meant to be considered protocol members?
|
|
96
|
+
* for d, e in enumerate(range(42)):
|
|
97
|
+
* pass
|
|
98
|
+
*
|
|
99
|
+
* class SubProto(BaseProto, Protocol):
|
|
100
|
+
* a = 42 # fine (declared in superclass)
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export type DetectsProtocolClassesWithAmbiguousInterfaces = Level;
|
|
104
|
+
/**
|
|
105
|
+
* ## What it does
|
|
106
|
+
* Checks for `assert_type()` calls where the actual type
|
|
107
|
+
* is an unspellable subtype of the asserted type.
|
|
108
|
+
*
|
|
109
|
+
* ## Why is this bad?
|
|
110
|
+
* `assert_type()` is intended to ensure that the inferred type of a value
|
|
111
|
+
* is exactly the same as the asserted type. But in some situations, ty
|
|
112
|
+
* has nonstandard extensions to the type system that allow it to infer
|
|
113
|
+
* more precise types than can be expressed in user annotations. ty emits a
|
|
114
|
+
* different error code to `type-assertion-failure` in these situations so
|
|
115
|
+
* that users can easily differentiate between the two cases.
|
|
116
|
+
*
|
|
117
|
+
* ## Example
|
|
118
|
+
*
|
|
119
|
+
* ```python
|
|
120
|
+
* def _(x: int):
|
|
121
|
+
* assert_type(x, int) # fine
|
|
122
|
+
* if x:
|
|
123
|
+
* assert_type(x, int) # error: [assert-type-unspellable-subtype]
|
|
124
|
+
* # the actual type is `int & ~AlwaysFalsy`,
|
|
125
|
+
* # which excludes types like `Literal[0]`
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
export type DetectsFailedTypeAssertions = Level;
|
|
129
|
+
/**
|
|
130
|
+
* ## What it does
|
|
131
|
+
* Checks for byte-strings in type annotation positions.
|
|
132
|
+
*
|
|
133
|
+
* ## Why is this bad?
|
|
134
|
+
* Static analysis tools like ty can't analyze type annotations that use byte-string notation.
|
|
135
|
+
*
|
|
136
|
+
* ## Examples
|
|
137
|
+
* ```python
|
|
138
|
+
* def test(): -> b"int":
|
|
139
|
+
* ...
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* Use instead:
|
|
143
|
+
* ```python
|
|
144
|
+
* def test(): -> "int":
|
|
145
|
+
* ...
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
export type DetectsByteStringsInTypeAnnotationPositions = Level;
|
|
149
|
+
/**
|
|
150
|
+
* ## What it does
|
|
151
|
+
* Checks for calls to abstract `@classmethod`s or `@staticmethod`s
|
|
152
|
+
* with "trivial bodies" when accessed on the class object itself.
|
|
153
|
+
*
|
|
154
|
+
* "Trivial bodies" are bodies that solely consist of `...`, `pass`,
|
|
155
|
+
* a docstring, and/or `raise NotImplementedError`.
|
|
156
|
+
*
|
|
157
|
+
* ## Why is this bad?
|
|
158
|
+
* An abstract method with a trivial body has no concrete implementation
|
|
159
|
+
* to execute, so calling such a method directly on the class will probably
|
|
160
|
+
* not have the desired effect.
|
|
161
|
+
*
|
|
162
|
+
* It is also unsound to call these methods directly on the class. Unlike
|
|
163
|
+
* other methods, ty permits abstract methods with trivial bodies to have
|
|
164
|
+
* non-`None` return types even though they always return `None` at runtime.
|
|
165
|
+
* This is because it is expected that these methods will always be
|
|
166
|
+
* overridden rather than being called directly. As a result of this
|
|
167
|
+
* exception to the normal rule, ty may infer an incorrect type if one of
|
|
168
|
+
* these methods is called directly, which may then mean that type errors
|
|
169
|
+
* elsewhere in your code go undetected by ty.
|
|
170
|
+
*
|
|
171
|
+
* Calling abstract classmethods or staticmethods via `type[X]` is allowed,
|
|
172
|
+
* since the actual runtime type could be a concrete subclass with an implementation.
|
|
173
|
+
*
|
|
174
|
+
* ## Example
|
|
175
|
+
* ```python
|
|
176
|
+
* from abc import ABC, abstractmethod
|
|
177
|
+
*
|
|
178
|
+
* class Foo(ABC):
|
|
179
|
+
* @classmethod
|
|
180
|
+
* @abstractmethod
|
|
181
|
+
* def method(cls) -> int: ...
|
|
182
|
+
*
|
|
183
|
+
* Foo.method() # Error: cannot call abstract classmethod
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
export type DetectsCallsToAbstractMethodsWithTrivialBodiesOnClassObjects = Level;
|
|
187
|
+
/**
|
|
188
|
+
* ## What it does
|
|
189
|
+
* Checks for calls to non-callable objects.
|
|
190
|
+
*
|
|
191
|
+
* ## Why is this bad?
|
|
192
|
+
* Calling a non-callable object will raise a `TypeError` at runtime.
|
|
193
|
+
*
|
|
194
|
+
* ## Examples
|
|
195
|
+
* ```python
|
|
196
|
+
* 4() # TypeError: 'int' object is not callable
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
export type DetectsCallsToNonCallableObjects = Level;
|
|
200
|
+
/**
|
|
201
|
+
* ## What it does
|
|
202
|
+
* Checks for calls to objects typed as `Top[Callable[..., T]]` (the infinite union of all
|
|
203
|
+
* callable types with return type `T`).
|
|
204
|
+
*
|
|
205
|
+
* ## Why is this bad?
|
|
206
|
+
* When an object is narrowed to `Top[Callable[..., object]]` (e.g., via `callable(x)` or
|
|
207
|
+
* `isinstance(x, Callable)`), we know the object is callable, but we don't know its
|
|
208
|
+
* precise signature. This type represents the set of all possible callable types
|
|
209
|
+
* (including, e.g., functions that take no arguments and functions that require arguments),
|
|
210
|
+
* so no specific set of arguments can be guaranteed to be valid.
|
|
211
|
+
*
|
|
212
|
+
* ## Examples
|
|
213
|
+
* ```python
|
|
214
|
+
* def f(x: object):
|
|
215
|
+
* if callable(x):
|
|
216
|
+
* x() # error: We know `x` is callable, but not what arguments it accepts
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
export type DetectsCallsToTheTopCallableType = Level;
|
|
220
|
+
/**
|
|
221
|
+
* ## What it does
|
|
222
|
+
* Checks whether an argument is used as both a value and a type form in a call.
|
|
223
|
+
*
|
|
224
|
+
* ## Why is this bad?
|
|
225
|
+
* Such calls have confusing semantics and often indicate a logic error.
|
|
226
|
+
*
|
|
227
|
+
* ## Examples
|
|
228
|
+
* ```python
|
|
229
|
+
* from typing import reveal_type
|
|
230
|
+
* from ty_extensions import is_singleton
|
|
231
|
+
*
|
|
232
|
+
* if flag:
|
|
233
|
+
* f = repr # Expects a value
|
|
234
|
+
* else:
|
|
235
|
+
* f = is_singleton # Expects a type form
|
|
236
|
+
*
|
|
237
|
+
* f(int) # error
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
export type DetectsWhenAnArgumentIsUsedAsBothAValueAndATypeFormInACall = Level;
|
|
241
|
+
/**
|
|
242
|
+
* ## What it does
|
|
243
|
+
* Checks whether a variable has been declared as two conflicting types.
|
|
244
|
+
*
|
|
245
|
+
* ## Why is this bad
|
|
246
|
+
* A variable with two conflicting declarations likely indicates a mistake.
|
|
247
|
+
* Moreover, it could lead to incorrect or ill-defined type inference for
|
|
248
|
+
* other code that relies on these variables.
|
|
249
|
+
*
|
|
250
|
+
* ## Examples
|
|
251
|
+
* ```python
|
|
252
|
+
* if b:
|
|
253
|
+
* a: int
|
|
254
|
+
* else:
|
|
255
|
+
* a: str
|
|
256
|
+
*
|
|
257
|
+
* a = 1
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
export type DetectsConflictingDeclarations = Level;
|
|
261
|
+
/**
|
|
262
|
+
* ## What it does
|
|
263
|
+
* Checks for class definitions where the metaclass of the class
|
|
264
|
+
* being created would not be a subclass of the metaclasses of
|
|
265
|
+
* all the class's bases.
|
|
266
|
+
*
|
|
267
|
+
* ## Why is it bad?
|
|
268
|
+
* Such a class definition raises a `TypeError` at runtime.
|
|
269
|
+
*
|
|
270
|
+
* ## Examples
|
|
271
|
+
* ```python
|
|
272
|
+
* class M1(type): ...
|
|
273
|
+
* class M2(type): ...
|
|
274
|
+
* class A(metaclass=M1): ...
|
|
275
|
+
* class B(metaclass=M2): ...
|
|
276
|
+
*
|
|
277
|
+
* # TypeError: metaclass conflict
|
|
278
|
+
* class C(A, B): ...
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
281
|
+
export type DetectsConflictingMetaclasses = Level;
|
|
282
|
+
/**
|
|
283
|
+
* ## What it does
|
|
284
|
+
* Checks for class definitions in stub files that inherit
|
|
285
|
+
* (directly or indirectly) from themselves.
|
|
286
|
+
*
|
|
287
|
+
* ## Why is it bad?
|
|
288
|
+
* Although forward references are natively supported in stub files,
|
|
289
|
+
* inheritance cycles are still disallowed, as it is impossible to
|
|
290
|
+
* resolve a consistent [method resolution order] for a class that
|
|
291
|
+
* inherits from itself.
|
|
292
|
+
*
|
|
293
|
+
* ## Examples
|
|
294
|
+
* ```python
|
|
295
|
+
* # foo.pyi
|
|
296
|
+
* class A(B): ...
|
|
297
|
+
* class B(A): ...
|
|
298
|
+
* ```
|
|
299
|
+
*
|
|
300
|
+
* [method resolution order]: https://docs.python.org/3/glossary.html#term-method-resolution-order
|
|
301
|
+
*/
|
|
302
|
+
export type DetectsCyclicClassDefinitions = Level;
|
|
303
|
+
/**
|
|
304
|
+
* ## What it does
|
|
305
|
+
* Checks for type alias definitions that (directly or mutually) refer to themselves.
|
|
306
|
+
*
|
|
307
|
+
* ## Why is it bad?
|
|
308
|
+
* Although it is permitted to define a recursive type alias, it is not meaningful
|
|
309
|
+
* to have a type alias whose expansion can only result in itself, and is therefore not allowed.
|
|
310
|
+
*
|
|
311
|
+
* ## Examples
|
|
312
|
+
* ```python
|
|
313
|
+
* type Itself = Itself
|
|
314
|
+
*
|
|
315
|
+
* type A = B
|
|
316
|
+
* type B = A
|
|
317
|
+
* ```
|
|
318
|
+
*/
|
|
319
|
+
export type DetectsCyclicTypeAliasDefinitions = Level;
|
|
320
|
+
/**
|
|
321
|
+
* ## What it does
|
|
322
|
+
* Checks for dataclass definitions where required fields are defined after
|
|
323
|
+
* fields with default values.
|
|
324
|
+
*
|
|
325
|
+
* ## Why is this bad?
|
|
326
|
+
* In dataclasses, all required fields (fields without default values) must be
|
|
327
|
+
* defined before fields with default values. This is a Python requirement that
|
|
328
|
+
* will raise a `TypeError` at runtime if violated.
|
|
329
|
+
*
|
|
330
|
+
* ## Example
|
|
331
|
+
* ```python
|
|
332
|
+
* from dataclasses import dataclass
|
|
333
|
+
*
|
|
334
|
+
* @dataclass
|
|
335
|
+
* class Example:
|
|
336
|
+
* x: int = 1 # Field with default value
|
|
337
|
+
* y: str # Error: Required field after field with default
|
|
338
|
+
* ```
|
|
339
|
+
*/
|
|
340
|
+
export type DetectsDataclassDefinitionsWithRequiredFieldsAfterFieldsWithDefaultValues = Level;
|
|
341
|
+
/**
|
|
342
|
+
* ## What it does
|
|
343
|
+
* Checks for uses of deprecated items
|
|
344
|
+
*
|
|
345
|
+
* ## Why is this bad?
|
|
346
|
+
* Deprecated items should no longer be used.
|
|
347
|
+
*
|
|
348
|
+
* ## Examples
|
|
349
|
+
* ```python
|
|
350
|
+
* @warnings.deprecated("use new_func instead")
|
|
351
|
+
* def old_func(): ...
|
|
352
|
+
*
|
|
353
|
+
* old_func() # emits [deprecated] diagnostic
|
|
354
|
+
* ```
|
|
355
|
+
*/
|
|
356
|
+
export type DetectsUsesOfDeprecatedItems = Level;
|
|
357
|
+
/**
|
|
358
|
+
* ## What it does
|
|
359
|
+
* It detects division by zero.
|
|
360
|
+
*
|
|
361
|
+
* ## Why is this bad?
|
|
362
|
+
* Dividing by zero raises a `ZeroDivisionError` at runtime.
|
|
363
|
+
*
|
|
364
|
+
* ## Rule status
|
|
365
|
+
* This rule is currently disabled by default because of the number of
|
|
366
|
+
* false positives it can produce.
|
|
367
|
+
*
|
|
368
|
+
* ## Examples
|
|
369
|
+
* ```python
|
|
370
|
+
* 5 / 0
|
|
371
|
+
* ```
|
|
372
|
+
*/
|
|
373
|
+
export type DetectsDivisionByZero = Level;
|
|
374
|
+
/**
|
|
375
|
+
* ## What it does
|
|
376
|
+
* Checks for class definitions with duplicate bases.
|
|
377
|
+
*
|
|
378
|
+
* ## Why is this bad?
|
|
379
|
+
* Class definitions with duplicate bases raise `TypeError` at runtime.
|
|
380
|
+
*
|
|
381
|
+
* ## Examples
|
|
382
|
+
* ```python
|
|
383
|
+
* class A: ...
|
|
384
|
+
*
|
|
385
|
+
* # TypeError: duplicate base class
|
|
386
|
+
* class B(A, A): ...
|
|
387
|
+
* ```
|
|
388
|
+
*/
|
|
389
|
+
export type DetectsClassDefinitionsWithDuplicateBases = Level;
|
|
390
|
+
/**
|
|
391
|
+
* ## What it does
|
|
392
|
+
* Checks for dataclass definitions with more than one field
|
|
393
|
+
* annotated with `KW_ONLY`.
|
|
394
|
+
*
|
|
395
|
+
* ## Why is this bad?
|
|
396
|
+
* `dataclasses.KW_ONLY` is a special marker used to
|
|
397
|
+
* emulate the `*` syntax in normal signatures.
|
|
398
|
+
* It can only be used once per dataclass.
|
|
399
|
+
*
|
|
400
|
+
* Attempting to annotate two different fields with
|
|
401
|
+
* it will lead to a runtime error.
|
|
402
|
+
*
|
|
403
|
+
* ## Examples
|
|
404
|
+
* ```python
|
|
405
|
+
* from dataclasses import dataclass, KW_ONLY
|
|
406
|
+
*
|
|
407
|
+
* @dataclass
|
|
408
|
+
* class A: # Crash at runtime
|
|
409
|
+
* b: int
|
|
410
|
+
* _1: KW_ONLY
|
|
411
|
+
* c: str
|
|
412
|
+
* _2: KW_ONLY
|
|
413
|
+
* d: bytes
|
|
414
|
+
* ```
|
|
415
|
+
*/
|
|
416
|
+
export type DetectsDataclassDefinitionsWithMoreThanOneUsageOfKW_ONLY = Level;
|
|
417
|
+
/**
|
|
418
|
+
* ## What it does
|
|
419
|
+
* Detects functions with empty bodies that have a non-`None` return type annotation.
|
|
420
|
+
*
|
|
421
|
+
* The errors reported by this rule have the same motivation as the `invalid-return-type`
|
|
422
|
+
* rule. The diagnostic exists as a separate error code to allow users to disable this
|
|
423
|
+
* rule while prototyping code. While we strongly recommend enabling this rule if
|
|
424
|
+
* possible, users migrating from other type checkers may also find it useful to
|
|
425
|
+
* temporarily disable this rule on some or all of their codebase if they find it
|
|
426
|
+
* results in a large number of diagnostics.
|
|
427
|
+
*
|
|
428
|
+
* ## Why is this bad?
|
|
429
|
+
* A function with an empty body (containing only `...`, `pass`, or a docstring) will
|
|
430
|
+
* implicitly return `None` at runtime. Returning `None` when the return type is non-`None`
|
|
431
|
+
* is unsound, and will lead to ty inferring incorrect types elsewhere.
|
|
432
|
+
*
|
|
433
|
+
* Functions with empty bodies are permitted in certain contexts where they serve as
|
|
434
|
+
* declarations rather than implementations:
|
|
435
|
+
*
|
|
436
|
+
* - Functions in stub files (`.pyi`)
|
|
437
|
+
* - Methods in Protocol classes
|
|
438
|
+
* - Abstract methods decorated with `@abstractmethod`
|
|
439
|
+
* - Overload declarations decorated with `@overload`
|
|
440
|
+
* - Functions in `if TYPE_CHECKING` blocks
|
|
441
|
+
*
|
|
442
|
+
* ## Examples
|
|
443
|
+
* ```python
|
|
444
|
+
* def foo() -> int: ... # error: [empty-body]
|
|
445
|
+
*
|
|
446
|
+
* def bar() -> str:
|
|
447
|
+
* """A function that does nothing."""
|
|
448
|
+
* pass # error: [empty-body]
|
|
449
|
+
* ```
|
|
450
|
+
*/
|
|
451
|
+
export type DetectsFunctionsWithEmptyBodiesThatHaveANonNoneReturnTypeAnnotation = Level;
|
|
452
|
+
/**
|
|
453
|
+
* ## What it does
|
|
454
|
+
* Checks for forward annotations that contain escape characters.
|
|
455
|
+
*
|
|
456
|
+
* ## Why is this bad?
|
|
457
|
+
* Static analysis tools like ty can't analyze type annotations that contain escape characters.
|
|
458
|
+
*
|
|
459
|
+
* ## Example
|
|
460
|
+
*
|
|
461
|
+
* ```python
|
|
462
|
+
* def foo() -> "intt\b": ...
|
|
463
|
+
* ```
|
|
464
|
+
*/
|
|
465
|
+
export type DetectsForwardTypeAnnotationsWithEscapeCharacters = Level;
|
|
466
|
+
/**
|
|
467
|
+
* ## What it does
|
|
468
|
+
* Checks for `@final` decorators applied to non-method functions.
|
|
469
|
+
*
|
|
470
|
+
* ## Why is this bad?
|
|
471
|
+
* The `@final` decorator is only meaningful on methods and classes.
|
|
472
|
+
* Applying it to a module-level function or a nested function has no
|
|
473
|
+
* effect and is likely a mistake.
|
|
474
|
+
*
|
|
475
|
+
* ## Example
|
|
476
|
+
*
|
|
477
|
+
* ```python
|
|
478
|
+
* from typing import final
|
|
479
|
+
*
|
|
480
|
+
* # Error: @final is not allowed on non-method functions
|
|
481
|
+
* @final
|
|
482
|
+
* def my_function() -> int:
|
|
483
|
+
* return 0
|
|
484
|
+
* ```
|
|
485
|
+
*/
|
|
486
|
+
export type DetectsFinalAppliedToNonMethodFunctions = Level;
|
|
487
|
+
/**
|
|
488
|
+
* ## What it does
|
|
489
|
+
* Checks for `Final` symbols that are declared without a value and are never
|
|
490
|
+
* assigned a value in their scope.
|
|
491
|
+
*
|
|
492
|
+
* ## Why is this bad?
|
|
493
|
+
* A `Final` symbol must be initialized with a value at the time of declaration
|
|
494
|
+
* or in a subsequent assignment. At module or function scope, the assignment must
|
|
495
|
+
* occur in the same scope. In a class body, the assignment may occur in `__init__`.
|
|
496
|
+
*
|
|
497
|
+
* ## Examples
|
|
498
|
+
* ```python
|
|
499
|
+
* from typing import Final
|
|
500
|
+
*
|
|
501
|
+
* # Error: `Final` symbol without a value
|
|
502
|
+
* MY_CONSTANT: Final[int]
|
|
503
|
+
*
|
|
504
|
+
* # OK: `Final` symbol with a value
|
|
505
|
+
* MY_CONSTANT: Final[int] = 1
|
|
506
|
+
* ```
|
|
507
|
+
*/
|
|
508
|
+
export type DetectsFinalDeclarationsWithoutAValue = Level;
|
|
509
|
+
/**
|
|
510
|
+
* ## What it does
|
|
511
|
+
* Checks for f-strings in type annotation positions.
|
|
512
|
+
*
|
|
513
|
+
* ## Why is this bad?
|
|
514
|
+
* Static analysis tools like ty can't analyze type annotations that use f-string notation.
|
|
515
|
+
*
|
|
516
|
+
* ## Examples
|
|
517
|
+
* ```python
|
|
518
|
+
* def test(): -> f"int":
|
|
519
|
+
* ...
|
|
520
|
+
* ```
|
|
521
|
+
*
|
|
522
|
+
* Use instead:
|
|
523
|
+
* ```python
|
|
524
|
+
* def test(): -> "int":
|
|
525
|
+
* ...
|
|
526
|
+
* ```
|
|
527
|
+
*/
|
|
528
|
+
export type DetectsFStringsInTypeAnnotationPositions = Level;
|
|
529
|
+
/**
|
|
530
|
+
* ## What it does
|
|
531
|
+
* Checks for `ty: ignore[code]` where `code` isn't a known lint rule.
|
|
532
|
+
*
|
|
533
|
+
* ## Why is this bad?
|
|
534
|
+
* A `ty: ignore[code]` directive with a `code` that doesn't match
|
|
535
|
+
* any known rule will not suppress any type errors, and is probably a mistake.
|
|
536
|
+
*
|
|
537
|
+
* ## Examples
|
|
538
|
+
* ```py
|
|
539
|
+
* a = 20 / 0 # ty: ignore[division-by-zer]
|
|
540
|
+
* ```
|
|
541
|
+
*
|
|
542
|
+
* Use instead:
|
|
543
|
+
*
|
|
544
|
+
* ```py
|
|
545
|
+
* a = 20 / 0 # ty: ignore[division-by-zero]
|
|
546
|
+
* ```
|
|
547
|
+
*/
|
|
548
|
+
export type DetectsTyIgnoreCommentsThatReferenceUnknownRules = Level;
|
|
549
|
+
/**
|
|
550
|
+
* ## What it does
|
|
551
|
+
* Checks for implicit concatenated strings in type annotation positions.
|
|
552
|
+
*
|
|
553
|
+
* ## Why is this bad?
|
|
554
|
+
* Static analysis tools like ty can't analyze type annotations that use implicit concatenated strings.
|
|
555
|
+
*
|
|
556
|
+
* ## Examples
|
|
557
|
+
* ```python
|
|
558
|
+
* def test(): -> "Literal[" "5" "]":
|
|
559
|
+
* ...
|
|
560
|
+
* ```
|
|
561
|
+
*
|
|
562
|
+
* Use instead:
|
|
563
|
+
* ```python
|
|
564
|
+
* def test(): -> "Literal[5]":
|
|
565
|
+
* ...
|
|
566
|
+
* ```
|
|
567
|
+
*/
|
|
568
|
+
export type DetectsImplicitConcatenatedStringsInTypeAnnotations = Level;
|
|
569
|
+
/**
|
|
570
|
+
* ## What it does
|
|
571
|
+
* Checks for classes with an inconsistent [method resolution order] (MRO).
|
|
572
|
+
*
|
|
573
|
+
* ## Why is this bad?
|
|
574
|
+
* Classes with an inconsistent MRO will raise a `TypeError` at runtime.
|
|
575
|
+
*
|
|
576
|
+
* ## Examples
|
|
577
|
+
* ```python
|
|
578
|
+
* class A: ...
|
|
579
|
+
* class B(A): ...
|
|
580
|
+
*
|
|
581
|
+
* # TypeError: Cannot create a consistent method resolution order
|
|
582
|
+
* class C(A, B): ...
|
|
583
|
+
* ```
|
|
584
|
+
*
|
|
585
|
+
* [method resolution order]: https://docs.python.org/3/glossary.html#term-method-resolution-order
|
|
586
|
+
*/
|
|
587
|
+
export type DetectsClassDefinitionsWithAnInconsistentMRO = Level;
|
|
588
|
+
/**
|
|
589
|
+
* ## What it does
|
|
590
|
+
* Checks for attempts to use an out of bounds index to get an item from
|
|
591
|
+
* a container.
|
|
592
|
+
*
|
|
593
|
+
* ## Why is this bad?
|
|
594
|
+
* Using an out of bounds index will raise an `IndexError` at runtime.
|
|
595
|
+
*
|
|
596
|
+
* ## Examples
|
|
597
|
+
* ```python
|
|
598
|
+
* t = (0, 1, 2)
|
|
599
|
+
* t[3] # IndexError: tuple index out of range
|
|
600
|
+
* ```
|
|
601
|
+
*/
|
|
602
|
+
export type DetectsIndexOutOfBoundsErrors = Level;
|
|
603
|
+
/**
|
|
604
|
+
* ## What it does
|
|
605
|
+
* Checks for calls to `final()` that type checkers cannot interpret.
|
|
606
|
+
*
|
|
607
|
+
* ## Why is this bad?
|
|
608
|
+
* The `final()` function is designed to be used as a decorator. When called directly
|
|
609
|
+
* as a function (e.g., `final(type(...))`), type checkers will not understand the
|
|
610
|
+
* application of `final` and will not prevent subclassing.
|
|
611
|
+
*
|
|
612
|
+
* ## Example
|
|
613
|
+
*
|
|
614
|
+
* ```python
|
|
615
|
+
* from typing import final
|
|
616
|
+
*
|
|
617
|
+
* # Incorrect: type checkers will not prevent subclassing
|
|
618
|
+
* MyClass = final(type("MyClass", (), {}))
|
|
619
|
+
*
|
|
620
|
+
* # Correct: use `final` as a decorator
|
|
621
|
+
* @final
|
|
622
|
+
* class MyClass: ...
|
|
623
|
+
* ```
|
|
624
|
+
*/
|
|
625
|
+
export type DetectsCallsToFinalThatTypeCheckersCannotInterpret = Level;
|
|
626
|
+
/**
|
|
627
|
+
* ## What it does
|
|
628
|
+
* Checks for classes definitions which will fail at runtime due to
|
|
629
|
+
* "instance memory layout conflicts".
|
|
630
|
+
*
|
|
631
|
+
* This error is usually caused by attempting to combine multiple classes
|
|
632
|
+
* that define non-empty `__slots__` in a class's [Method Resolution Order]
|
|
633
|
+
* (MRO), or by attempting to combine multiple builtin classes in a class's
|
|
634
|
+
* MRO.
|
|
635
|
+
*
|
|
636
|
+
* ## Why is this bad?
|
|
637
|
+
* Inheriting from bases with conflicting instance memory layouts
|
|
638
|
+
* will lead to a `TypeError` at runtime.
|
|
639
|
+
*
|
|
640
|
+
* An instance memory layout conflict occurs when CPython cannot determine
|
|
641
|
+
* the memory layout instances of a class should have, because the instance
|
|
642
|
+
* memory layout of one of its bases conflicts with the instance memory layout
|
|
643
|
+
* of one or more of its other bases.
|
|
644
|
+
*
|
|
645
|
+
* For example, if a Python class defines non-empty `__slots__`, this will
|
|
646
|
+
* impact the memory layout of instances of that class. Multiple inheritance
|
|
647
|
+
* from more than one different class defining non-empty `__slots__` is not
|
|
648
|
+
* allowed:
|
|
649
|
+
*
|
|
650
|
+
* ```python
|
|
651
|
+
* class A:
|
|
652
|
+
* __slots__ = ("a", "b")
|
|
653
|
+
*
|
|
654
|
+
* class B:
|
|
655
|
+
* __slots__ = ("a", "b") # Even if the values are the same
|
|
656
|
+
*
|
|
657
|
+
* # TypeError: multiple bases have instance lay-out conflict
|
|
658
|
+
* class C(A, B): ...
|
|
659
|
+
* ```
|
|
660
|
+
*
|
|
661
|
+
* An instance layout conflict can also be caused by attempting to use
|
|
662
|
+
* multiple inheritance with two builtin classes, due to the way that these
|
|
663
|
+
* classes are implemented in a CPython C extension:
|
|
664
|
+
*
|
|
665
|
+
* ```python
|
|
666
|
+
* class A(int, float): ... # TypeError: multiple bases have instance lay-out conflict
|
|
667
|
+
* ```
|
|
668
|
+
*
|
|
669
|
+
* Note that pure-Python classes with no `__slots__`, or pure-Python classes
|
|
670
|
+
* with empty `__slots__`, are always compatible:
|
|
671
|
+
*
|
|
672
|
+
* ```python
|
|
673
|
+
* class A: ...
|
|
674
|
+
* class B:
|
|
675
|
+
* __slots__ = ()
|
|
676
|
+
* class C:
|
|
677
|
+
* __slots__ = ("a", "b")
|
|
678
|
+
*
|
|
679
|
+
* # fine
|
|
680
|
+
* class D(A, B, C): ...
|
|
681
|
+
* ```
|
|
682
|
+
*
|
|
683
|
+
* ## Known problems
|
|
684
|
+
* Classes that have "dynamic" definitions of `__slots__` (definitions do not consist
|
|
685
|
+
* of string literals, or tuples of string literals) are not currently considered disjoint
|
|
686
|
+
* bases by ty.
|
|
687
|
+
*
|
|
688
|
+
* Additionally, this check is not exhaustive: many C extensions (including several in
|
|
689
|
+
* the standard library) define classes that use extended memory layouts and thus cannot
|
|
690
|
+
* coexist in a single MRO. Since it is currently not possible to represent this fact in
|
|
691
|
+
* stub files, having a full knowledge of these classes is also impossible. When it comes
|
|
692
|
+
* to classes that do not define `__slots__` at the Python level, therefore, ty, currently
|
|
693
|
+
* only hard-codes a number of cases where it knows that a class will produce instances with
|
|
694
|
+
* an atypical memory layout.
|
|
695
|
+
*
|
|
696
|
+
* ## Further reading
|
|
697
|
+
* - [CPython documentation: `__slots__`](https://docs.python.org/3/reference/datamodel.html#slots)
|
|
698
|
+
* - [CPython documentation: Method Resolution Order](https://docs.python.org/3/glossary.html#term-method-resolution-order)
|
|
699
|
+
*
|
|
700
|
+
* [Method Resolution Order]: https://docs.python.org/3/glossary.html#term-method-resolution-order
|
|
701
|
+
*/
|
|
702
|
+
export type DetectsClassDefinitionsThatRaiseTypeErrorDueToInstanceLayoutConflict = Level;
|
|
703
|
+
/**
|
|
704
|
+
* ## What it does
|
|
705
|
+
* Detects call arguments whose type is not assignable to the corresponding typed parameter.
|
|
706
|
+
*
|
|
707
|
+
* ## Why is this bad?
|
|
708
|
+
* Passing an argument of a type the function (or callable object) does not accept violates
|
|
709
|
+
* the expectations of the function author and may cause unexpected runtime errors within the
|
|
710
|
+
* body of the function.
|
|
711
|
+
*
|
|
712
|
+
* ## Examples
|
|
713
|
+
* ```python
|
|
714
|
+
* def func(x: int): ...
|
|
715
|
+
* func("foo") # error: [invalid-argument-type]
|
|
716
|
+
* ```
|
|
717
|
+
*/
|
|
718
|
+
export type DetectsCallArgumentsWhoseTypeIsNotAssignableToTheCorrespondingTypedParameter = Level;
|
|
719
|
+
/**
|
|
720
|
+
* ## What it does
|
|
721
|
+
* Checks for assignments where the type of the value
|
|
722
|
+
* is not [assignable to] the type of the assignee.
|
|
723
|
+
*
|
|
724
|
+
* ## Why is this bad?
|
|
725
|
+
* Such assignments break the rules of the type system and
|
|
726
|
+
* weaken a type checker's ability to accurately reason about your code.
|
|
727
|
+
*
|
|
728
|
+
* ## Examples
|
|
729
|
+
* ```python
|
|
730
|
+
* a: int = ''
|
|
731
|
+
* ```
|
|
732
|
+
*
|
|
733
|
+
* [assignable to]: https://typing.python.org/en/latest/spec/glossary.html#term-assignable
|
|
734
|
+
*/
|
|
735
|
+
export type DetectsInvalidAssignments = Level;
|
|
736
|
+
/**
|
|
737
|
+
* ## What it does
|
|
738
|
+
* Checks for assignments to class variables from instances
|
|
739
|
+
* and assignments to instance variables from its class.
|
|
740
|
+
*
|
|
741
|
+
* ## Why is this bad?
|
|
742
|
+
* Incorrect assignments break the rules of the type system and
|
|
743
|
+
* weaken a type checker's ability to accurately reason about your code.
|
|
744
|
+
*
|
|
745
|
+
* ## Examples
|
|
746
|
+
* ```python
|
|
747
|
+
* class C:
|
|
748
|
+
* class_var: ClassVar[int] = 1
|
|
749
|
+
* instance_var: int
|
|
750
|
+
*
|
|
751
|
+
* C.class_var = 3 # okay
|
|
752
|
+
* C().class_var = 3 # error: Cannot assign to class variable
|
|
753
|
+
*
|
|
754
|
+
* C().instance_var = 3 # okay
|
|
755
|
+
* C.instance_var = 3 # error: Cannot assign to instance variable
|
|
756
|
+
* ```
|
|
757
|
+
*/
|
|
758
|
+
export type InvalidAttributeAccess = Level;
|
|
759
|
+
/**
|
|
760
|
+
* ## What it does
|
|
761
|
+
* Checks for `await` being used with types that are not [Awaitable].
|
|
762
|
+
*
|
|
763
|
+
* ## Why is this bad?
|
|
764
|
+
* Such expressions will lead to `TypeError` being raised at runtime.
|
|
765
|
+
*
|
|
766
|
+
* ## Examples
|
|
767
|
+
* ```python
|
|
768
|
+
* import asyncio
|
|
769
|
+
*
|
|
770
|
+
* class InvalidAwait:
|
|
771
|
+
* def __await__(self) -> int:
|
|
772
|
+
* return 5
|
|
773
|
+
*
|
|
774
|
+
* async def main() -> None:
|
|
775
|
+
* await InvalidAwait() # error: [invalid-await]
|
|
776
|
+
* await 42 # error: [invalid-await]
|
|
777
|
+
*
|
|
778
|
+
* asyncio.run(main())
|
|
779
|
+
* ```
|
|
780
|
+
*
|
|
781
|
+
* [Awaitable]: https://docs.python.org/3/library/collections.abc.html#collections.abc.Awaitable
|
|
782
|
+
*/
|
|
783
|
+
export type DetectsAwaitingOnTypesThatDonTSupportIt = Level;
|
|
784
|
+
/**
|
|
785
|
+
* ## What it does
|
|
786
|
+
* Checks for class definitions that have bases which are not instances of `type`.
|
|
787
|
+
*
|
|
788
|
+
* ## Why is this bad?
|
|
789
|
+
* Class definitions with bases like this will lead to `TypeError` being raised at runtime.
|
|
790
|
+
*
|
|
791
|
+
* ## Examples
|
|
792
|
+
* ```python
|
|
793
|
+
* class A(42): ... # error: [invalid-base]
|
|
794
|
+
* ```
|
|
795
|
+
*/
|
|
796
|
+
export type DetectsClassBasesThatWillCauseTheClassDefinitionToRaiseAnExceptionAtRuntime = Level;
|
|
797
|
+
/**
|
|
798
|
+
* ## What it does
|
|
799
|
+
* Checks for expressions used in `with` statements
|
|
800
|
+
* that do not implement the context manager protocol.
|
|
801
|
+
*
|
|
802
|
+
* ## Why is this bad?
|
|
803
|
+
* Such a statement will raise `TypeError` at runtime.
|
|
804
|
+
*
|
|
805
|
+
* ## Examples
|
|
806
|
+
* ```python
|
|
807
|
+
* # TypeError: 'int' object does not support the context manager protocol
|
|
808
|
+
* with 1:
|
|
809
|
+
* print(2)
|
|
810
|
+
* ```
|
|
811
|
+
*/
|
|
812
|
+
export type DetectsExpressionsUsedInWithStatementsThatDonTImplementTheContextManagerProtocol = Level;
|
|
813
|
+
/**
|
|
814
|
+
* ## What it does
|
|
815
|
+
* Checks for invalid applications of the `@dataclass` decorator.
|
|
816
|
+
*
|
|
817
|
+
* ## Why is this bad?
|
|
818
|
+
* Applying `@dataclass` to a class that inherits from `NamedTuple`, `TypedDict`,
|
|
819
|
+
* `Enum`, or `Protocol` is invalid:
|
|
820
|
+
*
|
|
821
|
+
* - `NamedTuple` and `TypedDict` classes will raise an exception at runtime when
|
|
822
|
+
* instantiating the class.
|
|
823
|
+
* - `Enum` classes with `@dataclass` are [explicitly not supported].
|
|
824
|
+
* - `Protocol` classes define interfaces and cannot be instantiated.
|
|
825
|
+
*
|
|
826
|
+
* ## Examples
|
|
827
|
+
* ```python
|
|
828
|
+
* from dataclasses import dataclass
|
|
829
|
+
* from typing import NamedTuple
|
|
830
|
+
*
|
|
831
|
+
* @dataclass # error: [invalid-dataclass]
|
|
832
|
+
* class Foo(NamedTuple):
|
|
833
|
+
* x: int
|
|
834
|
+
* ```
|
|
835
|
+
*
|
|
836
|
+
* [explicitly not supported]: https://docs.python.org/3/howto/enum.html#dataclass-support
|
|
837
|
+
*/
|
|
838
|
+
export type DetectsInvalidDataclassApplications = Level;
|
|
839
|
+
/**
|
|
840
|
+
* ## What it does
|
|
841
|
+
* Checks for dataclass definitions that have both `frozen=True` and a custom `__setattr__` or
|
|
842
|
+
* `__delattr__` method defined.
|
|
843
|
+
*
|
|
844
|
+
* ## Why is this bad?
|
|
845
|
+
* Frozen dataclasses synthesize `__setattr__` and `__delattr__` methods which raise a
|
|
846
|
+
* `FrozenInstanceError` to emulate immutability.
|
|
847
|
+
*
|
|
848
|
+
* Overriding either of these methods raises a runtime error.
|
|
849
|
+
*
|
|
850
|
+
* ## Examples
|
|
851
|
+
* ```python
|
|
852
|
+
* from dataclasses import dataclass
|
|
853
|
+
*
|
|
854
|
+
* @dataclass(frozen=True)
|
|
855
|
+
* class A:
|
|
856
|
+
* def __setattr__(self, name: str, value: object) -> None: ...
|
|
857
|
+
* ```
|
|
858
|
+
*/
|
|
859
|
+
export type DetectsDataclassesWithFrozenTrueThatHaveACustom_Setattr__Or_Delattr__Implementation = Level;
|
|
860
|
+
/**
|
|
861
|
+
* ## What it does
|
|
862
|
+
* Checks for declarations where the inferred type of an existing symbol
|
|
863
|
+
* is not [assignable to] its post-hoc declared type.
|
|
864
|
+
*
|
|
865
|
+
* ## Why is this bad?
|
|
866
|
+
* Such declarations break the rules of the type system and
|
|
867
|
+
* weaken a type checker's ability to accurately reason about your code.
|
|
868
|
+
*
|
|
869
|
+
* ## Examples
|
|
870
|
+
* ```python
|
|
871
|
+
* a = 1
|
|
872
|
+
* a: str
|
|
873
|
+
* ```
|
|
874
|
+
*
|
|
875
|
+
* [assignable to]: https://typing.python.org/en/latest/spec/glossary.html#term-assignable
|
|
876
|
+
*/
|
|
877
|
+
export type DetectsInvalidDeclarations = Level;
|
|
878
|
+
/**
|
|
879
|
+
* ## What it does
|
|
880
|
+
* Checks for enum members that have explicit type annotations.
|
|
881
|
+
*
|
|
882
|
+
* ## Why is this bad?
|
|
883
|
+
* The [typing spec] states that type checkers should infer a literal type
|
|
884
|
+
* for all enum members. An explicit type annotation on an enum member is
|
|
885
|
+
* misleading because the annotated type will be incorrect — the actual
|
|
886
|
+
* runtime type is the enum class itself, not the annotated type.
|
|
887
|
+
*
|
|
888
|
+
* In CPython's `enum` module, annotated assignments with values are still
|
|
889
|
+
* treated as members at runtime, but the annotation will confuse readers of the code.
|
|
890
|
+
*
|
|
891
|
+
* ## Examples
|
|
892
|
+
* ```python
|
|
893
|
+
* from enum import Enum
|
|
894
|
+
*
|
|
895
|
+
* class Pet(Enum):
|
|
896
|
+
* CAT = 1 # OK
|
|
897
|
+
* DOG: int = 2 # Error: enum members should not be annotated
|
|
898
|
+
* ```
|
|
899
|
+
*
|
|
900
|
+
* Use instead:
|
|
901
|
+
* ```python
|
|
902
|
+
* from enum import Enum
|
|
903
|
+
*
|
|
904
|
+
* class Pet(Enum):
|
|
905
|
+
* CAT = 1
|
|
906
|
+
* DOG = 2
|
|
907
|
+
* ```
|
|
908
|
+
*
|
|
909
|
+
* ## References
|
|
910
|
+
* - [Typing spec: Enum members](https://typing.python.org/en/latest/spec/enums.html#enum-members)
|
|
911
|
+
*
|
|
912
|
+
* [typing spec]: https://typing.python.org/en/latest/spec/enums.html#enum-members
|
|
913
|
+
*/
|
|
914
|
+
export type DetectsTypeAnnotationsOnEnumMembers = Level;
|
|
915
|
+
/**
|
|
916
|
+
* ## What it does
|
|
917
|
+
* Checks for exception handlers that catch non-exception classes.
|
|
918
|
+
*
|
|
919
|
+
* ## Why is this bad?
|
|
920
|
+
* Catching classes that do not inherit from `BaseException` will raise a `TypeError` at runtime.
|
|
921
|
+
*
|
|
922
|
+
* ## Example
|
|
923
|
+
* ```python
|
|
924
|
+
* try:
|
|
925
|
+
* 1 / 0
|
|
926
|
+
* except 1:
|
|
927
|
+
* ...
|
|
928
|
+
* ```
|
|
929
|
+
*
|
|
930
|
+
* Use instead:
|
|
931
|
+
* ```python
|
|
932
|
+
* try:
|
|
933
|
+
* 1 / 0
|
|
934
|
+
* except ZeroDivisionError:
|
|
935
|
+
* ...
|
|
936
|
+
* ```
|
|
937
|
+
*
|
|
938
|
+
* ## References
|
|
939
|
+
* - [Python documentation: except clause](https://docs.python.org/3/reference/compound_stmts.html#except-clause)
|
|
940
|
+
* - [Python documentation: Built-in Exceptions](https://docs.python.org/3/library/exceptions.html#built-in-exceptions)
|
|
941
|
+
*
|
|
942
|
+
* ## Ruff rule
|
|
943
|
+
* This rule corresponds to Ruff's [`except-with-non-exception-classes` (`B030`)](https://docs.astral.sh/ruff/rules/except-with-non-exception-classes)
|
|
944
|
+
*/
|
|
945
|
+
export type DetectsExceptionHandlersThatCatchClassesThatDoNotInheritFromBaseException = Level;
|
|
946
|
+
/**
|
|
947
|
+
* ## What it does
|
|
948
|
+
* Checks for methods that are decorated with `@override` but do not override any method in a superclass.
|
|
949
|
+
*
|
|
950
|
+
* ## Why is this bad?
|
|
951
|
+
* Decorating a method with `@override` declares to the type checker that the intention is that it should
|
|
952
|
+
* override a method from a superclass.
|
|
953
|
+
*
|
|
954
|
+
* ## Example
|
|
955
|
+
*
|
|
956
|
+
* ```python
|
|
957
|
+
* from typing import override
|
|
958
|
+
*
|
|
959
|
+
* class A:
|
|
960
|
+
* @override
|
|
961
|
+
* def foo(self): ... # Error raised here
|
|
962
|
+
*
|
|
963
|
+
* class B(A):
|
|
964
|
+
* @override
|
|
965
|
+
* def ffooo(self): ... # Error raised here
|
|
966
|
+
*
|
|
967
|
+
* class C:
|
|
968
|
+
* @override
|
|
969
|
+
* def __repr__(self): ... # fine: overrides `object.__repr__`
|
|
970
|
+
*
|
|
971
|
+
* class D(A):
|
|
972
|
+
* @override
|
|
973
|
+
* def foo(self): ... # fine: overrides `A.foo`
|
|
974
|
+
* ```
|
|
975
|
+
*/
|
|
976
|
+
export type DetectsMethodsThatAreDecoratedWithOverrideButDoNotOverrideAnyMethodInASuperclass = Level;
|
|
977
|
+
/**
|
|
978
|
+
* ## What it does
|
|
979
|
+
* Checks for dataclasses with invalid frozen inheritance:
|
|
980
|
+
* - A frozen dataclass cannot inherit from a non-frozen dataclass.
|
|
981
|
+
* - A non-frozen dataclass cannot inherit from a frozen dataclass.
|
|
982
|
+
*
|
|
983
|
+
* ## Why is this bad?
|
|
984
|
+
* Python raises a `TypeError` at runtime when either of these inheritance
|
|
985
|
+
* patterns occurs.
|
|
986
|
+
*
|
|
987
|
+
* ## Example
|
|
988
|
+
*
|
|
989
|
+
* ```python
|
|
990
|
+
* from dataclasses import dataclass
|
|
991
|
+
*
|
|
992
|
+
* @dataclass
|
|
993
|
+
* class Base:
|
|
994
|
+
* x: int
|
|
995
|
+
*
|
|
996
|
+
* @dataclass(frozen=True)
|
|
997
|
+
* class Child(Base): # Error raised here
|
|
998
|
+
* y: int
|
|
999
|
+
*
|
|
1000
|
+
* @dataclass(frozen=True)
|
|
1001
|
+
* class FrozenBase:
|
|
1002
|
+
* x: int
|
|
1003
|
+
*
|
|
1004
|
+
* @dataclass
|
|
1005
|
+
* class NonFrozenChild(FrozenBase): # Error raised here
|
|
1006
|
+
* y: int
|
|
1007
|
+
* ```
|
|
1008
|
+
*/
|
|
1009
|
+
export type DetectsDataclassesWithInvalidFrozenNonFrozenSubclassing = Level;
|
|
1010
|
+
/**
|
|
1011
|
+
* ## What it does
|
|
1012
|
+
* Checks for the creation of invalid generic classes
|
|
1013
|
+
*
|
|
1014
|
+
* ## Why is this bad?
|
|
1015
|
+
* There are several requirements that you must follow when defining a generic class.
|
|
1016
|
+
* Many of these result in `TypeError` being raised at runtime if they are violated.
|
|
1017
|
+
*
|
|
1018
|
+
* ## Examples
|
|
1019
|
+
* ```python
|
|
1020
|
+
* from typing_extensions import Generic, TypeVar
|
|
1021
|
+
*
|
|
1022
|
+
* T = TypeVar("T")
|
|
1023
|
+
* U = TypeVar("U", default=int)
|
|
1024
|
+
*
|
|
1025
|
+
* # error: class uses both PEP-695 syntax and legacy syntax
|
|
1026
|
+
* class C[U](Generic[T]): ...
|
|
1027
|
+
*
|
|
1028
|
+
* # error: type parameter with default comes before type parameter without default
|
|
1029
|
+
* class D(Generic[U, T]): ...
|
|
1030
|
+
* ```
|
|
1031
|
+
*
|
|
1032
|
+
* ## References
|
|
1033
|
+
* - [Typing spec: Generics](https://typing.python.org/en/latest/spec/generics.html#introduction)
|
|
1034
|
+
*/
|
|
1035
|
+
export type DetectsInvalidGenericClasses = Level;
|
|
1036
|
+
/**
|
|
1037
|
+
* ## What it does
|
|
1038
|
+
* Checks for enum classes that are also generic.
|
|
1039
|
+
*
|
|
1040
|
+
* ## Why is this bad?
|
|
1041
|
+
* Enum classes cannot be generic. Python does not support generic enums:
|
|
1042
|
+
* attempting to create one will either result in an immediate `TypeError`
|
|
1043
|
+
* at runtime, or will create a class that cannot be specialized in the way
|
|
1044
|
+
* that a normal generic class can.
|
|
1045
|
+
*
|
|
1046
|
+
* ## Examples
|
|
1047
|
+
* ```python
|
|
1048
|
+
* from enum import Enum
|
|
1049
|
+
* from typing import Generic, TypeVar
|
|
1050
|
+
*
|
|
1051
|
+
* T = TypeVar("T")
|
|
1052
|
+
*
|
|
1053
|
+
* # error: enum class cannot be generic (class creation fails with `TypeError`)
|
|
1054
|
+
* class E[T](Enum):
|
|
1055
|
+
* A = 1
|
|
1056
|
+
*
|
|
1057
|
+
* # error: enum class cannot be generic (class creation fails with `TypeError`)
|
|
1058
|
+
* class F(Enum, Generic[T]):
|
|
1059
|
+
* A = 1
|
|
1060
|
+
*
|
|
1061
|
+
* # error: enum class cannot be generic -- the class creation does not immediately fail...
|
|
1062
|
+
* class G(Generic[T], Enum):
|
|
1063
|
+
* A = 1
|
|
1064
|
+
*
|
|
1065
|
+
* # ...but this raises `KeyError`:
|
|
1066
|
+
* x: G[int]
|
|
1067
|
+
* ```
|
|
1068
|
+
*
|
|
1069
|
+
* ## References
|
|
1070
|
+
* - [Python documentation: Enum](https://docs.python.org/3/library/enum.html)
|
|
1071
|
+
*/
|
|
1072
|
+
export type DetectsGenericEnumClasses = Level;
|
|
1073
|
+
/**
|
|
1074
|
+
* ## What it does
|
|
1075
|
+
* Checks for `type: ignore` and `ty: ignore` comments that are syntactically incorrect.
|
|
1076
|
+
*
|
|
1077
|
+
* ## Why is this bad?
|
|
1078
|
+
* A syntactically incorrect ignore comment is probably a mistake and is useless.
|
|
1079
|
+
*
|
|
1080
|
+
* ## Examples
|
|
1081
|
+
* ```py
|
|
1082
|
+
* a = 20 / 0 # type: ignoree
|
|
1083
|
+
* ```
|
|
1084
|
+
*
|
|
1085
|
+
* Use instead:
|
|
1086
|
+
*
|
|
1087
|
+
* ```py
|
|
1088
|
+
* a = 20 / 0 # type: ignore
|
|
1089
|
+
* ```
|
|
1090
|
+
*/
|
|
1091
|
+
export type DetectsIgnoreCommentsThatUseInvalidSyntax = Level;
|
|
1092
|
+
/**
|
|
1093
|
+
* ## What it does
|
|
1094
|
+
* Checks for subscript accesses with invalid keys and `TypedDict` construction with an
|
|
1095
|
+
* unknown key.
|
|
1096
|
+
*
|
|
1097
|
+
* ## Why is this bad?
|
|
1098
|
+
* Subscripting with an invalid key will raise a `KeyError` at runtime.
|
|
1099
|
+
*
|
|
1100
|
+
* Creating a `TypedDict` with an unknown key is likely a mistake; if the `TypedDict` is
|
|
1101
|
+
* `closed=true` it also violates the expectations of the type.
|
|
1102
|
+
*
|
|
1103
|
+
* ## Examples
|
|
1104
|
+
* ```python
|
|
1105
|
+
* from typing import TypedDict
|
|
1106
|
+
*
|
|
1107
|
+
* class Person(TypedDict):
|
|
1108
|
+
* name: str
|
|
1109
|
+
* age: int
|
|
1110
|
+
*
|
|
1111
|
+
* alice = Person(name="Alice", age=30)
|
|
1112
|
+
* alice["height"] # KeyError: 'height'
|
|
1113
|
+
*
|
|
1114
|
+
* bob: Person = { "name": "Bob", "age": 30 } # typo!
|
|
1115
|
+
*
|
|
1116
|
+
* carol = Person(name="Carol", age=25) # typo!
|
|
1117
|
+
* ```
|
|
1118
|
+
*/
|
|
1119
|
+
export type DetectsInvalidSubscriptAccessesOrTypedDictLiteralKeys = Level;
|
|
1120
|
+
/**
|
|
1121
|
+
* ## What it does
|
|
1122
|
+
*
|
|
1123
|
+
* Checks for parameters that appear to be attempting to use the legacy convention
|
|
1124
|
+
* to specify that a parameter is positional-only, but do so incorrectly.
|
|
1125
|
+
*
|
|
1126
|
+
* The "legacy convention" for specifying positional-only parameters was
|
|
1127
|
+
* specified in [PEP 484]. It states that parameters with names starting with
|
|
1128
|
+
* `__` should be considered positional-only by type checkers. [PEP 570], introduced
|
|
1129
|
+
* in Python 3.8, added dedicated syntax for specifying positional-only parameters,
|
|
1130
|
+
* rendering the legacy convention obsolete. However, some codebases may still
|
|
1131
|
+
* use the legacy convention for compatibility with older Python versions.
|
|
1132
|
+
*
|
|
1133
|
+
* ## Why is this bad?
|
|
1134
|
+
*
|
|
1135
|
+
* In most cases, a type checker will not consider a parameter to be positional-only
|
|
1136
|
+
* if it comes after a positional-or-keyword parameter, even if its name starts with
|
|
1137
|
+
* `__`. This may be unexpected to the author of the code.
|
|
1138
|
+
*
|
|
1139
|
+
* ## Example
|
|
1140
|
+
*
|
|
1141
|
+
* ```python
|
|
1142
|
+
* def f(x, __y): # Error: `__y` is not considered positional-only
|
|
1143
|
+
* pass
|
|
1144
|
+
* ```
|
|
1145
|
+
*
|
|
1146
|
+
* Use instead:
|
|
1147
|
+
*
|
|
1148
|
+
* ```python
|
|
1149
|
+
* def f(__x, __y): # If you need compatibility with Python <=3.7
|
|
1150
|
+
* pass
|
|
1151
|
+
* ```
|
|
1152
|
+
*
|
|
1153
|
+
* or:
|
|
1154
|
+
*
|
|
1155
|
+
* ```python
|
|
1156
|
+
* def f(x, y, /): # Python 3.8+ syntax
|
|
1157
|
+
* pass
|
|
1158
|
+
* ```
|
|
1159
|
+
*
|
|
1160
|
+
* ## References
|
|
1161
|
+
*
|
|
1162
|
+
* - [Typing spec: positional-only parameters (legacy syntax)](https://typing.python.org/en/latest/spec/historical.html#pos-only-double-underscore)
|
|
1163
|
+
* - [Python glossary: parameters](https://docs.python.org/3/glossary.html#term-parameter)
|
|
1164
|
+
*
|
|
1165
|
+
* [PEP 484]: https://peps.python.org/pep-0484/#positional-only-arguments
|
|
1166
|
+
* [PEP 570]: https://peps.python.org/pep-0570/
|
|
1167
|
+
*/
|
|
1168
|
+
export type DetectsIncorrectUsageOfTheLegacyConventionForSpecifyingPositionalOnlyParameters = Level;
|
|
1169
|
+
/**
|
|
1170
|
+
* ## What it does
|
|
1171
|
+
* Checks for the creation of invalid legacy `TypeVar`s
|
|
1172
|
+
*
|
|
1173
|
+
* ## Why is this bad?
|
|
1174
|
+
* There are several requirements that you must follow when creating a legacy `TypeVar`.
|
|
1175
|
+
*
|
|
1176
|
+
* ## Examples
|
|
1177
|
+
* ```python
|
|
1178
|
+
* from typing import TypeVar
|
|
1179
|
+
*
|
|
1180
|
+
* T = TypeVar("T") # okay
|
|
1181
|
+
* Q = TypeVar("S") # error: TypeVar name must match the variable it's assigned to
|
|
1182
|
+
* T = TypeVar("T") # error: TypeVars should not be redefined
|
|
1183
|
+
*
|
|
1184
|
+
* # error: TypeVar must be immediately assigned to a variable
|
|
1185
|
+
* def f(t: TypeVar("U")): ...
|
|
1186
|
+
* ```
|
|
1187
|
+
*
|
|
1188
|
+
* ## References
|
|
1189
|
+
* - [Typing spec: Generics](https://typing.python.org/en/latest/spec/generics.html#introduction)
|
|
1190
|
+
*/
|
|
1191
|
+
export type DetectsInvalidLegacyTypeVariables = Level;
|
|
1192
|
+
/**
|
|
1193
|
+
* ## What it does
|
|
1194
|
+
* Checks for invalid match patterns.
|
|
1195
|
+
*
|
|
1196
|
+
* ## Why is this bad?
|
|
1197
|
+
* Matching on invalid patterns will lead to a runtime error.
|
|
1198
|
+
*
|
|
1199
|
+
* ## Examples
|
|
1200
|
+
* ```python
|
|
1201
|
+
* NotAClass = 42
|
|
1202
|
+
*
|
|
1203
|
+
* match x:
|
|
1204
|
+
* case NotAClass(): # TypeError at runtime: must be a class
|
|
1205
|
+
* ...
|
|
1206
|
+
* ```
|
|
1207
|
+
*/
|
|
1208
|
+
export type DetectInvalidMatchPatterns = Level;
|
|
1209
|
+
/**
|
|
1210
|
+
* ## What it does
|
|
1211
|
+
* Checks for arguments to `metaclass=` that are invalid.
|
|
1212
|
+
*
|
|
1213
|
+
* ## Why is this bad?
|
|
1214
|
+
* Python allows arbitrary expressions to be used as the argument to `metaclass=`.
|
|
1215
|
+
* These expressions, however, need to be callable and accept the same arguments
|
|
1216
|
+
* as `type.__new__`.
|
|
1217
|
+
*
|
|
1218
|
+
* ## Example
|
|
1219
|
+
*
|
|
1220
|
+
* ```python
|
|
1221
|
+
* def f(): ...
|
|
1222
|
+
*
|
|
1223
|
+
* # TypeError: f() takes 0 positional arguments but 3 were given
|
|
1224
|
+
* class B(metaclass=f): ...
|
|
1225
|
+
* ```
|
|
1226
|
+
*
|
|
1227
|
+
* ## References
|
|
1228
|
+
* - [Python documentation: Metaclasses](https://docs.python.org/3/reference/datamodel.html#metaclasses)
|
|
1229
|
+
*/
|
|
1230
|
+
export type DetectsInvalidMetaclassArguments = Level;
|
|
1231
|
+
/**
|
|
1232
|
+
* ## What it does
|
|
1233
|
+
* Detects method overrides that violate the [Liskov Substitution Principle] ("LSP").
|
|
1234
|
+
*
|
|
1235
|
+
* The LSP states that an instance of a subtype should be substitutable for an instance of its supertype.
|
|
1236
|
+
* Applied to Python, this means:
|
|
1237
|
+
* 1. All argument combinations a superclass method accepts
|
|
1238
|
+
* must also be accepted by an overriding subclass method.
|
|
1239
|
+
* 2. The return type of an overriding subclass method must be a subtype
|
|
1240
|
+
* of the return type of the superclass method.
|
|
1241
|
+
*
|
|
1242
|
+
* ## Why is this bad?
|
|
1243
|
+
* Violating the Liskov Substitution Principle will lead to many of ty's assumptions and
|
|
1244
|
+
* inferences being incorrect, which will mean that it will fail to catch many possible
|
|
1245
|
+
* type errors in your code.
|
|
1246
|
+
*
|
|
1247
|
+
* ## Example
|
|
1248
|
+
* ```python
|
|
1249
|
+
* class Super:
|
|
1250
|
+
* def method(self, x) -> int:
|
|
1251
|
+
* return 42
|
|
1252
|
+
*
|
|
1253
|
+
* class Sub(Super):
|
|
1254
|
+
* # Liskov violation: `str` is not a subtype of `int`,
|
|
1255
|
+
* # but the supertype method promises to return an `int`.
|
|
1256
|
+
* def method(self, x) -> str: # error: [invalid-override]
|
|
1257
|
+
* return "foo"
|
|
1258
|
+
*
|
|
1259
|
+
* def accepts_super(s: Super) -> int:
|
|
1260
|
+
* return s.method(x=42)
|
|
1261
|
+
*
|
|
1262
|
+
* accepts_super(Sub()) # The result of this call is a string, but ty will infer
|
|
1263
|
+
* # it to be an `int` due to the violation of the Liskov Substitution Principle.
|
|
1264
|
+
*
|
|
1265
|
+
* class Sub2(Super):
|
|
1266
|
+
* # Liskov violation: the superclass method can be called with a `x=`
|
|
1267
|
+
* # keyword argument, but the subclass method does not accept it.
|
|
1268
|
+
* def method(self, y) -> int: # error: [invalid-override]
|
|
1269
|
+
* return 42
|
|
1270
|
+
*
|
|
1271
|
+
* accepts_super(Sub2()) # TypeError at runtime: method() got an unexpected keyword argument 'x'
|
|
1272
|
+
* # ty cannot catch this error due to the violation of the Liskov Substitution Principle.
|
|
1273
|
+
* ```
|
|
1274
|
+
*
|
|
1275
|
+
* ## Common issues
|
|
1276
|
+
*
|
|
1277
|
+
* ### Why does ty complain about my `__eq__` method?
|
|
1278
|
+
*
|
|
1279
|
+
* `__eq__` and `__ne__` methods in Python are generally expected to accept arbitrary
|
|
1280
|
+
* objects as their second argument, for example:
|
|
1281
|
+
*
|
|
1282
|
+
* ```python
|
|
1283
|
+
* class A:
|
|
1284
|
+
* x: int
|
|
1285
|
+
*
|
|
1286
|
+
* def __eq__(self, other: object) -> bool:
|
|
1287
|
+
* # gracefully handle an object of an unexpected type
|
|
1288
|
+
* # without raising an exception
|
|
1289
|
+
* if not isinstance(other, A):
|
|
1290
|
+
* return False
|
|
1291
|
+
* return self.x == other.x
|
|
1292
|
+
* ```
|
|
1293
|
+
*
|
|
1294
|
+
* If `A.__eq__` here were annotated as only accepting `A` instances for its second argument,
|
|
1295
|
+
* it would imply that you wouldn't be able to use `==` between instances of `A` and
|
|
1296
|
+
* instances of unrelated classes without an exception possibly being raised. While some
|
|
1297
|
+
* classes in Python do indeed behave this way, the strongly held convention is that it should
|
|
1298
|
+
* be avoided wherever possible. As part of this check, therefore, ty enforces that `__eq__`
|
|
1299
|
+
* and `__ne__` methods accept `object` as their second argument.
|
|
1300
|
+
*
|
|
1301
|
+
* ### Why does ty disagree with Ruff about how to write my method?
|
|
1302
|
+
*
|
|
1303
|
+
* Ruff has several rules that will encourage you to rename a parameter, or change its type
|
|
1304
|
+
* signature, if it thinks you're falling into a certain anti-pattern. For example, Ruff's
|
|
1305
|
+
* [ARG002](https://docs.astral.sh/ruff/rules/unused-method-argument/) rule recommends that an
|
|
1306
|
+
* unused parameter should either be removed or renamed to start with `_`. Applying either of
|
|
1307
|
+
* these suggestions can cause ty to start reporting an `invalid-method-override` error if
|
|
1308
|
+
* the function in question is a method on a subclass that overrides a method on a superclass,
|
|
1309
|
+
* and the change would cause the subclass method to no longer accept all argument combinations
|
|
1310
|
+
* that the superclass method accepts.
|
|
1311
|
+
*
|
|
1312
|
+
* This can usually be resolved by adding [`@typing.override`][override] to your method
|
|
1313
|
+
* definition. Ruff knows that a method decorated with `@typing.override` is intended to
|
|
1314
|
+
* override a method by the same name on a superclass, and avoids reporting rules like ARG002
|
|
1315
|
+
* for such methods; it knows that the changes recommended by ARG002 would violate the Liskov
|
|
1316
|
+
* Substitution Principle.
|
|
1317
|
+
*
|
|
1318
|
+
* Correct use of `@override` is enforced by ty's `invalid-explicit-override` rule.
|
|
1319
|
+
*
|
|
1320
|
+
* [Liskov Substitution Principle]: https://en.wikipedia.org/wiki/Liskov_substitution_principle
|
|
1321
|
+
* [override]: https://docs.python.org/3/library/typing.html#typing.override
|
|
1322
|
+
*/
|
|
1323
|
+
export type DetectsMethodDefinitionsThatViolateTheLiskovSubstitutionPrinciple = Level;
|
|
1324
|
+
/**
|
|
1325
|
+
* ## What it does
|
|
1326
|
+
* Checks for invalidly defined `NamedTuple` classes.
|
|
1327
|
+
*
|
|
1328
|
+
* ## Why is this bad?
|
|
1329
|
+
* An invalidly defined `NamedTuple` class may lead to the type checker
|
|
1330
|
+
* drawing incorrect conclusions. It may also lead to `TypeError`s or
|
|
1331
|
+
* `AttributeError`s at runtime.
|
|
1332
|
+
*
|
|
1333
|
+
* ## Examples
|
|
1334
|
+
* A class definition cannot combine `NamedTuple` with other base classes
|
|
1335
|
+
* in multiple inheritance; doing so raises a `TypeError` at runtime. The sole
|
|
1336
|
+
* exception to this rule is `Generic[]`, which can be used alongside `NamedTuple`
|
|
1337
|
+
* in a class's bases list.
|
|
1338
|
+
*
|
|
1339
|
+
* ```pycon
|
|
1340
|
+
* >>> from typing import NamedTuple
|
|
1341
|
+
* >>> class Foo(NamedTuple, object): ...
|
|
1342
|
+
* TypeError: can only inherit from a NamedTuple type and Generic
|
|
1343
|
+
* ```
|
|
1344
|
+
*
|
|
1345
|
+
* Further, `NamedTuple` field names cannot start with an underscore:
|
|
1346
|
+
*
|
|
1347
|
+
* ```pycon
|
|
1348
|
+
* >>> from typing import NamedTuple
|
|
1349
|
+
* >>> class Foo(NamedTuple):
|
|
1350
|
+
* ... _bar: int
|
|
1351
|
+
* ValueError: Field names cannot start with an underscore: '_bar'
|
|
1352
|
+
* ```
|
|
1353
|
+
*
|
|
1354
|
+
* `NamedTuple` classes also have certain synthesized attributes (like `_asdict`, `_make`,
|
|
1355
|
+
* `_replace`, etc.) that cannot be overwritten. Attempting to assign to these attributes
|
|
1356
|
+
* without a type annotation will raise an `AttributeError` at runtime.
|
|
1357
|
+
*
|
|
1358
|
+
* ```pycon
|
|
1359
|
+
* >>> from typing import NamedTuple
|
|
1360
|
+
* >>> class Foo(NamedTuple):
|
|
1361
|
+
* ... x: int
|
|
1362
|
+
* ... _asdict = 42
|
|
1363
|
+
* AttributeError: Cannot overwrite NamedTuple attribute _asdict
|
|
1364
|
+
* ```
|
|
1365
|
+
*/
|
|
1366
|
+
export type DetectsInvalidNamedTupleClassDefinitions = Level;
|
|
1367
|
+
/**
|
|
1368
|
+
* ## What it does
|
|
1369
|
+
* Checks for the creation of invalid `NewType`s
|
|
1370
|
+
*
|
|
1371
|
+
* ## Why is this bad?
|
|
1372
|
+
* There are several requirements that you must follow when creating a `NewType`.
|
|
1373
|
+
*
|
|
1374
|
+
* ## Examples
|
|
1375
|
+
* ```python
|
|
1376
|
+
* from typing import NewType
|
|
1377
|
+
*
|
|
1378
|
+
* def get_name() -> str: ...
|
|
1379
|
+
*
|
|
1380
|
+
* Foo = NewType("Foo", int) # okay
|
|
1381
|
+
* Bar = NewType(get_name(), int) # error: The first argument to `NewType` must be a string literal
|
|
1382
|
+
* Baz = NewType("Baz", int | str) # error: invalid base for `typing.NewType`
|
|
1383
|
+
* ```
|
|
1384
|
+
*/
|
|
1385
|
+
export type DetectsInvalidNewTypeDefinitions = Level;
|
|
1386
|
+
/**
|
|
1387
|
+
* ## What it does
|
|
1388
|
+
* Checks for various invalid `@overload` usages.
|
|
1389
|
+
*
|
|
1390
|
+
* ## Why is this bad?
|
|
1391
|
+
* The `@overload` decorator is used to define functions and methods that accepts different
|
|
1392
|
+
* combinations of arguments and return different types based on the arguments passed. This is
|
|
1393
|
+
* mainly beneficial for type checkers. But, if the `@overload` usage is invalid, the type
|
|
1394
|
+
* checker may not be able to provide correct type information.
|
|
1395
|
+
*
|
|
1396
|
+
* ## Example
|
|
1397
|
+
*
|
|
1398
|
+
* Defining only one overload:
|
|
1399
|
+
*
|
|
1400
|
+
* ```py
|
|
1401
|
+
* from typing import overload
|
|
1402
|
+
*
|
|
1403
|
+
* @overload
|
|
1404
|
+
* def foo(x: int) -> int: ...
|
|
1405
|
+
* def foo(x: int | None) -> int | None:
|
|
1406
|
+
* return x
|
|
1407
|
+
* ```
|
|
1408
|
+
*
|
|
1409
|
+
* Or, not providing an implementation for the overloaded definition:
|
|
1410
|
+
*
|
|
1411
|
+
* ```py
|
|
1412
|
+
* from typing import overload
|
|
1413
|
+
*
|
|
1414
|
+
* @overload
|
|
1415
|
+
* def foo() -> None: ...
|
|
1416
|
+
* @overload
|
|
1417
|
+
* def foo(x: int) -> int: ...
|
|
1418
|
+
* ```
|
|
1419
|
+
*
|
|
1420
|
+
* ## References
|
|
1421
|
+
* - [Python documentation: `@overload`](https://docs.python.org/3/library/typing.html#typing.overload)
|
|
1422
|
+
*/
|
|
1423
|
+
export type DetectsInvalidOverloadUsages = Level;
|
|
1424
|
+
/**
|
|
1425
|
+
* ## What it does
|
|
1426
|
+
* Checks for default values that can't be
|
|
1427
|
+
* assigned to the parameter's annotated type.
|
|
1428
|
+
*
|
|
1429
|
+
* ## Why is this bad?
|
|
1430
|
+
* This breaks the rules of the type system and
|
|
1431
|
+
* weakens a type checker's ability to accurately reason about your code.
|
|
1432
|
+
*
|
|
1433
|
+
* ## Examples
|
|
1434
|
+
* ```python
|
|
1435
|
+
* def f(a: int = ''): ...
|
|
1436
|
+
* ```
|
|
1437
|
+
*/
|
|
1438
|
+
export type DetectsDefaultValuesThatCanTBeAssignedToTheParameterSAnnotatedType = Level;
|
|
1439
|
+
/**
|
|
1440
|
+
* ## What it does
|
|
1441
|
+
* Checks for the creation of invalid `ParamSpec`s
|
|
1442
|
+
*
|
|
1443
|
+
* ## Why is this bad?
|
|
1444
|
+
* There are several requirements that you must follow when creating a `ParamSpec`.
|
|
1445
|
+
*
|
|
1446
|
+
* ## Examples
|
|
1447
|
+
* ```python
|
|
1448
|
+
* from typing import ParamSpec
|
|
1449
|
+
*
|
|
1450
|
+
* P1 = ParamSpec("P1") # okay
|
|
1451
|
+
* P2 = ParamSpec("S2") # error: ParamSpec name must match the variable it's assigned to
|
|
1452
|
+
* ```
|
|
1453
|
+
*
|
|
1454
|
+
* ## References
|
|
1455
|
+
* - [Typing spec: ParamSpec](https://typing.python.org/en/latest/spec/generics.html#paramspec)
|
|
1456
|
+
*/
|
|
1457
|
+
export type DetectsInvalidParamSpecUsage = Level;
|
|
1458
|
+
/**
|
|
1459
|
+
* ## What it does
|
|
1460
|
+
* Checks for protocol classes that will raise `TypeError` at runtime.
|
|
1461
|
+
*
|
|
1462
|
+
* ## Why is this bad?
|
|
1463
|
+
* An invalidly defined protocol class may lead to the type checker inferring
|
|
1464
|
+
* unexpected things. It may also lead to `TypeError`s at runtime.
|
|
1465
|
+
*
|
|
1466
|
+
* ## Examples
|
|
1467
|
+
* A `Protocol` class cannot inherit from a non-`Protocol` class;
|
|
1468
|
+
* this raises a `TypeError` at runtime:
|
|
1469
|
+
*
|
|
1470
|
+
* ```pycon
|
|
1471
|
+
* >>> from typing import Protocol
|
|
1472
|
+
* >>> class Foo(int, Protocol): ...
|
|
1473
|
+
* ...
|
|
1474
|
+
* Traceback (most recent call last):
|
|
1475
|
+
* File "<python-input-1>", line 1, in <module>
|
|
1476
|
+
* class Foo(int, Protocol): ...
|
|
1477
|
+
* TypeError: Protocols can only inherit from other protocols, got <class 'int'>
|
|
1478
|
+
* ```
|
|
1479
|
+
*/
|
|
1480
|
+
export type DetectsInvalidProtocolClassDefinitions = Level;
|
|
1481
|
+
/**
|
|
1482
|
+
* Checks for `raise` statements that raise non-exceptions or use invalid
|
|
1483
|
+
* causes for their raised exceptions.
|
|
1484
|
+
*
|
|
1485
|
+
* ## Why is this bad?
|
|
1486
|
+
* Only subclasses or instances of `BaseException` can be raised.
|
|
1487
|
+
* For an exception's cause, the same rules apply, except that `None` is also
|
|
1488
|
+
* permitted. Violating these rules results in a `TypeError` at runtime.
|
|
1489
|
+
*
|
|
1490
|
+
* ## Examples
|
|
1491
|
+
* ```python
|
|
1492
|
+
* def f():
|
|
1493
|
+
* try:
|
|
1494
|
+
* something()
|
|
1495
|
+
* except NameError:
|
|
1496
|
+
* raise "oops!" from f
|
|
1497
|
+
*
|
|
1498
|
+
* def g():
|
|
1499
|
+
* raise NotImplemented from 42
|
|
1500
|
+
* ```
|
|
1501
|
+
*
|
|
1502
|
+
* Use instead:
|
|
1503
|
+
* ```python
|
|
1504
|
+
* def f():
|
|
1505
|
+
* try:
|
|
1506
|
+
* something()
|
|
1507
|
+
* except NameError as e:
|
|
1508
|
+
* raise RuntimeError("oops!") from e
|
|
1509
|
+
*
|
|
1510
|
+
* def g():
|
|
1511
|
+
* raise NotImplementedError from None
|
|
1512
|
+
* ```
|
|
1513
|
+
*
|
|
1514
|
+
* ## References
|
|
1515
|
+
* - [Python documentation: The `raise` statement](https://docs.python.org/3/reference/simple_stmts.html#raise)
|
|
1516
|
+
* - [Python documentation: Built-in Exceptions](https://docs.python.org/3/library/exceptions.html#built-in-exceptions)
|
|
1517
|
+
*/
|
|
1518
|
+
export type DetectsRaiseStatementsThatRaiseInvalidExceptionsOrUseInvalidCauses = Level;
|
|
1519
|
+
/**
|
|
1520
|
+
* ## What it does
|
|
1521
|
+
* Detects returned values that can't be assigned to the function's annotated return type.
|
|
1522
|
+
*
|
|
1523
|
+
* Note that the special case of a function with a non-`None` return type and an empty body
|
|
1524
|
+
* is handled by the separate `empty-body` error code.
|
|
1525
|
+
*
|
|
1526
|
+
* ## Why is this bad?
|
|
1527
|
+
* Returning an object of a type incompatible with the annotated return type
|
|
1528
|
+
* is unsound, and will lead to ty inferring incorrect types elsewhere.
|
|
1529
|
+
*
|
|
1530
|
+
* ## Examples
|
|
1531
|
+
* ```python
|
|
1532
|
+
* def func() -> int:
|
|
1533
|
+
* return "a" # error: [invalid-return-type]
|
|
1534
|
+
* ```
|
|
1535
|
+
*/
|
|
1536
|
+
export type DetectsReturnedValuesThatCanTBeAssignedToTheFunctionSAnnotatedReturnType = Level;
|
|
1537
|
+
/**
|
|
1538
|
+
* ## What it does
|
|
1539
|
+
* Detects `super()` calls where:
|
|
1540
|
+
* - the first argument is not a valid class literal, or
|
|
1541
|
+
* - the second argument is not an instance or subclass of the first argument.
|
|
1542
|
+
*
|
|
1543
|
+
* ## Why is this bad?
|
|
1544
|
+
* `super(type, obj)` expects:
|
|
1545
|
+
* - the first argument to be a class,
|
|
1546
|
+
* - and the second argument to satisfy one of the following:
|
|
1547
|
+
* - `isinstance(obj, type)` is `True`
|
|
1548
|
+
* - `issubclass(obj, type)` is `True`
|
|
1549
|
+
*
|
|
1550
|
+
* Violating this relationship will raise a `TypeError` at runtime.
|
|
1551
|
+
*
|
|
1552
|
+
* ## Examples
|
|
1553
|
+
* ```python
|
|
1554
|
+
* class A:
|
|
1555
|
+
* ...
|
|
1556
|
+
* class B(A):
|
|
1557
|
+
* ...
|
|
1558
|
+
*
|
|
1559
|
+
* super(A, B()) # it's okay! `A` satisfies `isinstance(B(), A)`
|
|
1560
|
+
*
|
|
1561
|
+
* super(A(), B()) # error: `A()` is not a class
|
|
1562
|
+
*
|
|
1563
|
+
* super(B, A()) # error: `A()` does not satisfy `isinstance(A(), B)`
|
|
1564
|
+
* super(B, A) # error: `A` does not satisfy `issubclass(A, B)`
|
|
1565
|
+
* ```
|
|
1566
|
+
*
|
|
1567
|
+
* ## References
|
|
1568
|
+
* - [Python documentation: super()](https://docs.python.org/3/library/functions.html#super)
|
|
1569
|
+
*/
|
|
1570
|
+
export type DetectsInvalidArgumentsForSuper = Level;
|
|
1571
|
+
/**
|
|
1572
|
+
* ## What it does
|
|
1573
|
+
* Checks for string-literal annotations where the string cannot be
|
|
1574
|
+
* parsed as a Python expression.
|
|
1575
|
+
*
|
|
1576
|
+
* ## Why is this bad?
|
|
1577
|
+
* Type annotations are expected to be Python expressions that
|
|
1578
|
+
* describe the expected type of a variable, parameter, attribute or
|
|
1579
|
+
* `return` statement.
|
|
1580
|
+
*
|
|
1581
|
+
* Type annotations are permitted to be string-literal expressions, in
|
|
1582
|
+
* order to enable forward references to names not yet defined.
|
|
1583
|
+
* However, it must be possible to parse the contents of that string
|
|
1584
|
+
* literal as a normal Python expression.
|
|
1585
|
+
*
|
|
1586
|
+
* ## Example
|
|
1587
|
+
*
|
|
1588
|
+
* ```python
|
|
1589
|
+
* def foo() -> "intstance of C":
|
|
1590
|
+
* return 42
|
|
1591
|
+
*
|
|
1592
|
+
* class C: ...
|
|
1593
|
+
* ```
|
|
1594
|
+
*
|
|
1595
|
+
* Use instead:
|
|
1596
|
+
*
|
|
1597
|
+
* ```python
|
|
1598
|
+
* def foo() -> "C":
|
|
1599
|
+
* return 42
|
|
1600
|
+
*
|
|
1601
|
+
* class C: ...
|
|
1602
|
+
* ```
|
|
1603
|
+
*
|
|
1604
|
+
* ## References
|
|
1605
|
+
* - [Typing spec: The meaning of annotations](https://typing.python.org/en/latest/spec/annotations.html#the-meaning-of-annotations)
|
|
1606
|
+
* - [Typing spec: String annotations](https://typing.python.org/en/latest/spec/annotations.html#string-annotations)
|
|
1607
|
+
*/
|
|
1608
|
+
export type DetectsInvalidSyntaxInForwardAnnotations = Level;
|
|
1609
|
+
/**
|
|
1610
|
+
* ## What it does
|
|
1611
|
+
* Checks for classes decorated with `@functools.total_ordering` that don't
|
|
1612
|
+
* define any ordering method (`__lt__`, `__le__`, `__gt__`, or `__ge__`).
|
|
1613
|
+
*
|
|
1614
|
+
* ## Why is this bad?
|
|
1615
|
+
* The `@total_ordering` decorator requires the class to define at least one
|
|
1616
|
+
* ordering method. If none is defined, Python raises a `ValueError` at runtime.
|
|
1617
|
+
*
|
|
1618
|
+
* ## Example
|
|
1619
|
+
*
|
|
1620
|
+
* ```python
|
|
1621
|
+
* from functools import total_ordering
|
|
1622
|
+
*
|
|
1623
|
+
* @total_ordering
|
|
1624
|
+
* class MyClass: # Error: no ordering method defined
|
|
1625
|
+
* def __eq__(self, other: object) -> bool:
|
|
1626
|
+
* return True
|
|
1627
|
+
* ```
|
|
1628
|
+
*
|
|
1629
|
+
* Use instead:
|
|
1630
|
+
*
|
|
1631
|
+
* ```python
|
|
1632
|
+
* from functools import total_ordering
|
|
1633
|
+
*
|
|
1634
|
+
* @total_ordering
|
|
1635
|
+
* class MyClass:
|
|
1636
|
+
* def __eq__(self, other: object) -> bool:
|
|
1637
|
+
* return True
|
|
1638
|
+
*
|
|
1639
|
+
* def __lt__(self, other: "MyClass") -> bool:
|
|
1640
|
+
* return True
|
|
1641
|
+
* ```
|
|
1642
|
+
*/
|
|
1643
|
+
export type DetectsTotalOrderingClassesWithoutAnOrderingMethod = Level;
|
|
1644
|
+
/**
|
|
1645
|
+
* ## What it does
|
|
1646
|
+
* Checks for the creation of invalid `TypeAliasType`s
|
|
1647
|
+
*
|
|
1648
|
+
* ## Why is this bad?
|
|
1649
|
+
* There are several requirements that you must follow when creating a `TypeAliasType`.
|
|
1650
|
+
*
|
|
1651
|
+
* ## Examples
|
|
1652
|
+
* ```python
|
|
1653
|
+
* from typing import TypeAliasType
|
|
1654
|
+
*
|
|
1655
|
+
* IntOrStr = TypeAliasType("IntOrStr", int | str) # okay
|
|
1656
|
+
* NewAlias = TypeAliasType(get_name(), int) # error: TypeAliasType name must be a string literal
|
|
1657
|
+
* ```
|
|
1658
|
+
*/
|
|
1659
|
+
export type DetectsInvalidTypeAliasTypeDefinitions = Level;
|
|
1660
|
+
/**
|
|
1661
|
+
* ## What it does
|
|
1662
|
+
* Checks for invalid type arguments in explicit type specialization.
|
|
1663
|
+
*
|
|
1664
|
+
* ## Why is this bad?
|
|
1665
|
+
* Providing the wrong number of type arguments or type arguments that don't
|
|
1666
|
+
* satisfy the type variable's bounds or constraints will lead to incorrect
|
|
1667
|
+
* type inference and may indicate a misunderstanding of the generic type's
|
|
1668
|
+
* interface.
|
|
1669
|
+
*
|
|
1670
|
+
* ## Examples
|
|
1671
|
+
*
|
|
1672
|
+
* Using legacy type variables:
|
|
1673
|
+
* ```python
|
|
1674
|
+
* from typing import Generic, TypeVar
|
|
1675
|
+
*
|
|
1676
|
+
* T1 = TypeVar('T1', int, str)
|
|
1677
|
+
* T2 = TypeVar('T2', bound=int)
|
|
1678
|
+
*
|
|
1679
|
+
* class Foo1(Generic[T1]): ...
|
|
1680
|
+
* class Foo2(Generic[T2]): ...
|
|
1681
|
+
*
|
|
1682
|
+
* Foo1[bytes] # error: bytes does not satisfy T1's constraints
|
|
1683
|
+
* Foo2[str] # error: str does not satisfy T2's bound
|
|
1684
|
+
* ```
|
|
1685
|
+
*
|
|
1686
|
+
* Using PEP 695 type variables:
|
|
1687
|
+
* ```python
|
|
1688
|
+
* class Foo[T]: ...
|
|
1689
|
+
* class Bar[T, U]: ...
|
|
1690
|
+
*
|
|
1691
|
+
* Foo[int, str] # error: too many arguments
|
|
1692
|
+
* Bar[int] # error: too few arguments
|
|
1693
|
+
* ```
|
|
1694
|
+
*/
|
|
1695
|
+
export type DetectsInvalidTypeArgumentsInGenericSpecialization = Level;
|
|
1696
|
+
/**
|
|
1697
|
+
* ## What it does
|
|
1698
|
+
* Checks for a value other than `False` assigned to the `TYPE_CHECKING` variable, or an
|
|
1699
|
+
* annotation not assignable from `bool`.
|
|
1700
|
+
*
|
|
1701
|
+
* ## Why is this bad?
|
|
1702
|
+
* The name `TYPE_CHECKING` is reserved for a flag that can be used to provide conditional
|
|
1703
|
+
* code seen only by the type checker, and not at runtime. Normally this flag is imported from
|
|
1704
|
+
* `typing` or `typing_extensions`, but it can also be defined locally. If defined locally, it
|
|
1705
|
+
* must be assigned the value `False` at runtime; the type checker will consider its value to
|
|
1706
|
+
* be `True`. If annotated, it must be annotated as a type that can accept `bool` values.
|
|
1707
|
+
*
|
|
1708
|
+
* ## Examples
|
|
1709
|
+
* ```python
|
|
1710
|
+
* TYPE_CHECKING: str
|
|
1711
|
+
* TYPE_CHECKING = ''
|
|
1712
|
+
* ```
|
|
1713
|
+
*/
|
|
1714
|
+
export type DetectsInvalidTYPE_CHECKINGConstantAssignments = Level;
|
|
1715
|
+
/**
|
|
1716
|
+
* ## What it does
|
|
1717
|
+
* Checks for expressions that are used as [type expressions]
|
|
1718
|
+
* but cannot validly be interpreted as such.
|
|
1719
|
+
*
|
|
1720
|
+
* ## Why is this bad?
|
|
1721
|
+
* Such expressions cannot be understood by ty.
|
|
1722
|
+
* In some cases, they might raise errors at runtime.
|
|
1723
|
+
*
|
|
1724
|
+
* ## Examples
|
|
1725
|
+
* ```python
|
|
1726
|
+
* from typing import Annotated
|
|
1727
|
+
*
|
|
1728
|
+
* a: type[1] # `1` is not a type
|
|
1729
|
+
* b: Annotated[int] # `Annotated` expects at least two arguments
|
|
1730
|
+
* ```
|
|
1731
|
+
* [type expressions]: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions
|
|
1732
|
+
*/
|
|
1733
|
+
export type DetectsInvalidTypeForms = Level;
|
|
1734
|
+
/**
|
|
1735
|
+
* ## What it does
|
|
1736
|
+
* Checks for type guard function calls without a valid target.
|
|
1737
|
+
*
|
|
1738
|
+
* ## Why is this bad?
|
|
1739
|
+
* The first non-keyword non-variadic argument to a type guard function
|
|
1740
|
+
* is its target and must map to a symbol.
|
|
1741
|
+
*
|
|
1742
|
+
* Starred (`is_str(*a)`), literal (`is_str(42)`) and other non-symbol-like
|
|
1743
|
+
* expressions are invalid as narrowing targets.
|
|
1744
|
+
*
|
|
1745
|
+
* ## Examples
|
|
1746
|
+
* ```python
|
|
1747
|
+
* from typing import TypeIs
|
|
1748
|
+
*
|
|
1749
|
+
* def f(v: object) -> TypeIs[int]: ...
|
|
1750
|
+
*
|
|
1751
|
+
* f() # Error
|
|
1752
|
+
* f(*a) # Error
|
|
1753
|
+
* f(10) # Error
|
|
1754
|
+
* ```
|
|
1755
|
+
*/
|
|
1756
|
+
export type DetectsTypeGuardFunctionCallsThatHasNoNarrowingEffect = Level;
|
|
1757
|
+
/**
|
|
1758
|
+
* ## What it does
|
|
1759
|
+
* Checks for type guard functions without
|
|
1760
|
+
* a first non-self-like non-keyword-only non-variadic parameter.
|
|
1761
|
+
*
|
|
1762
|
+
* ## Why is this bad?
|
|
1763
|
+
* Type narrowing functions must accept at least one positional argument
|
|
1764
|
+
* (non-static methods must accept another in addition to `self`/`cls`).
|
|
1765
|
+
*
|
|
1766
|
+
* Extra parameters/arguments are allowed but do not affect narrowing.
|
|
1767
|
+
*
|
|
1768
|
+
* ## Examples
|
|
1769
|
+
* ```python
|
|
1770
|
+
* from typing import TypeIs
|
|
1771
|
+
*
|
|
1772
|
+
* def f() -> TypeIs[int]: ... # Error, no parameter
|
|
1773
|
+
* def f(*, v: object) -> TypeIs[int]: ... # Error, no positional arguments allowed
|
|
1774
|
+
* def f(*args: object) -> TypeIs[int]: ... # Error, expect variadic arguments
|
|
1775
|
+
* class C:
|
|
1776
|
+
* def f(self) -> TypeIs[int]: ... # Error, only positional argument expected is `self`
|
|
1777
|
+
* ```
|
|
1778
|
+
*/
|
|
1779
|
+
export type DetectsMalformedTypeGuardFunctions = Level;
|
|
1780
|
+
/**
|
|
1781
|
+
* ## What it does
|
|
1782
|
+
* Checks for [type variables] whose bounds reference type variables.
|
|
1783
|
+
*
|
|
1784
|
+
* ## Why is this bad?
|
|
1785
|
+
* The bound of a type variable must be a concrete type.
|
|
1786
|
+
*
|
|
1787
|
+
* ## Examples
|
|
1788
|
+
* ```python
|
|
1789
|
+
* T = TypeVar('T', bound=list['T']) # error: [invalid-type-variable-bound]
|
|
1790
|
+
* U = TypeVar('U')
|
|
1791
|
+
* T = TypeVar('T', bound=U) # error: [invalid-type-variable-bound]
|
|
1792
|
+
*
|
|
1793
|
+
* def f[T: list[T]](): ... # error: [invalid-type-variable-bound]
|
|
1794
|
+
* def g[U, T: U](): ... # error: [invalid-type-variable-bound]
|
|
1795
|
+
* ```
|
|
1796
|
+
*
|
|
1797
|
+
* [type variable]: https://docs.python.org/3/library/typing.html#typing.TypeVar
|
|
1798
|
+
*/
|
|
1799
|
+
export type DetectsInvalidTypeVariableBounds = Level;
|
|
1800
|
+
/**
|
|
1801
|
+
* ## What it does
|
|
1802
|
+
*
|
|
1803
|
+
* Checks for constrained [type variables] with only one constraint,
|
|
1804
|
+
* or that those constraints reference type variables.
|
|
1805
|
+
*
|
|
1806
|
+
* ## Why is this bad?
|
|
1807
|
+
*
|
|
1808
|
+
* A constrained type variable must have at least two constraints.
|
|
1809
|
+
*
|
|
1810
|
+
* ## Examples
|
|
1811
|
+
*
|
|
1812
|
+
* ```python
|
|
1813
|
+
* from typing import TypeVar
|
|
1814
|
+
*
|
|
1815
|
+
* T = TypeVar('T', str) # invalid constrained TypeVar
|
|
1816
|
+
*
|
|
1817
|
+
* I = TypeVar('I', bound=int)
|
|
1818
|
+
* U = TypeVar('U', list[I], int) # invalid constrained TypeVar
|
|
1819
|
+
* ```
|
|
1820
|
+
*
|
|
1821
|
+
* Use instead:
|
|
1822
|
+
*
|
|
1823
|
+
* ```python
|
|
1824
|
+
* T = TypeVar('T', str, int) # valid constrained TypeVar
|
|
1825
|
+
*
|
|
1826
|
+
* # or
|
|
1827
|
+
*
|
|
1828
|
+
* T = TypeVar('T', bound=str) # valid bound TypeVar
|
|
1829
|
+
*
|
|
1830
|
+
* U = TypeVar('U', list[int], int) # valid constrained Type
|
|
1831
|
+
* ```
|
|
1832
|
+
*
|
|
1833
|
+
* [type variables]: https://docs.python.org/3/library/typing.html#typing.TypeVar
|
|
1834
|
+
*/
|
|
1835
|
+
export type DetectsInvalidTypeVariableConstraints = Level;
|
|
1836
|
+
/**
|
|
1837
|
+
* ## What it does
|
|
1838
|
+
* Checks for [type variables] whose default type is not compatible with
|
|
1839
|
+
* the type variable's bound or constraints.
|
|
1840
|
+
*
|
|
1841
|
+
* ## Why is this bad?
|
|
1842
|
+
* If a type variable has a bound, the default must be assignable to that
|
|
1843
|
+
* bound (see: [bound rules]). If a type variable has constraints, the default
|
|
1844
|
+
* must be one of the constraints (see: [constraint rules]).
|
|
1845
|
+
*
|
|
1846
|
+
* ## Examples
|
|
1847
|
+
* ```python
|
|
1848
|
+
* T = TypeVar("T", bound=str, default=int) # error: [invalid-type-variable-default]
|
|
1849
|
+
* U = TypeVar("U", int, str, default=bytes) # error: [invalid-type-variable-default]
|
|
1850
|
+
* ```
|
|
1851
|
+
*
|
|
1852
|
+
* [type variables]: https://docs.python.org/3/library/typing.html#typing.TypeVar
|
|
1853
|
+
* [bound rules]: https://typing.python.org/en/latest/spec/generics.html#bound-rules
|
|
1854
|
+
* [constraint rules]: https://typing.python.org/en/latest/spec/generics.html#constraint-rules
|
|
1855
|
+
*/
|
|
1856
|
+
export type DetectsInvalidTypeVariableDefaults = Level;
|
|
1857
|
+
/**
|
|
1858
|
+
* ## What it does
|
|
1859
|
+
* Detects errors in `TypedDict` class headers, such as unexpected arguments
|
|
1860
|
+
* or invalid base classes.
|
|
1861
|
+
*
|
|
1862
|
+
* ## Why is this bad?
|
|
1863
|
+
* The typing spec states that `TypedDict`s are not permitted to have
|
|
1864
|
+
* custom metaclasses. Using `**` unpacking in a `TypedDict` header
|
|
1865
|
+
* is also prohibited by ty, as it means that ty cannot statically determine
|
|
1866
|
+
* whether keys in the `TypedDict` are intended to be required or optional.
|
|
1867
|
+
*
|
|
1868
|
+
* ## Example
|
|
1869
|
+
* ```python
|
|
1870
|
+
* from typing import TypedDict
|
|
1871
|
+
*
|
|
1872
|
+
* class Foo(TypedDict, metaclass=whatever): # error: [invalid-typed-dict-header]
|
|
1873
|
+
* ...
|
|
1874
|
+
*
|
|
1875
|
+
* def f(x: dict):
|
|
1876
|
+
* class Bar(TypedDict, **x): # error: [invalid-typed-dict-header]
|
|
1877
|
+
* ...
|
|
1878
|
+
* ```
|
|
1879
|
+
*/
|
|
1880
|
+
export type DetectsInvalidStatementsInTypedDictClassHeaders = Level;
|
|
1881
|
+
/**
|
|
1882
|
+
* ## What it does
|
|
1883
|
+
* Detects statements other than annotated declarations in `TypedDict` class bodies.
|
|
1884
|
+
*
|
|
1885
|
+
* ## Why is this bad?
|
|
1886
|
+
* `TypedDict` class bodies aren't allowed to contain any other types of statements. For
|
|
1887
|
+
* example, method definitions and field values aren't allowed. None of these will be
|
|
1888
|
+
* available on "instances of the `TypedDict`" at runtime (as `dict` is the runtime class of
|
|
1889
|
+
* all "`TypedDict` instances").
|
|
1890
|
+
*
|
|
1891
|
+
* ## Example
|
|
1892
|
+
* ```python
|
|
1893
|
+
* from typing import TypedDict
|
|
1894
|
+
*
|
|
1895
|
+
* class Foo(TypedDict):
|
|
1896
|
+
* def bar(self): # error: [invalid-typed-dict-statement]
|
|
1897
|
+
* pass
|
|
1898
|
+
* ```
|
|
1899
|
+
*/
|
|
1900
|
+
export type DetectsInvalidStatementsInTypedDictClassBodies = Level;
|
|
1901
|
+
/**
|
|
1902
|
+
* ## What it does
|
|
1903
|
+
* Reports invalid runtime checks against `Protocol` classes.
|
|
1904
|
+
* This includes explicit calls `isinstance()`/`issubclass()` against
|
|
1905
|
+
* non-runtime-checkable protocols, `issubclass()` calls against protocols
|
|
1906
|
+
* that have non-method members, and implicit `isinstance()` checks against
|
|
1907
|
+
* non-runtime-checkable protocols via pattern matching.
|
|
1908
|
+
*
|
|
1909
|
+
* ## Why is this bad?
|
|
1910
|
+
* These calls (implicit or explicit) raise `TypeError` at runtime.
|
|
1911
|
+
*
|
|
1912
|
+
* ## Examples
|
|
1913
|
+
* ```python
|
|
1914
|
+
* from typing_extensions import Protocol, runtime_checkable
|
|
1915
|
+
*
|
|
1916
|
+
* class HasX(Protocol):
|
|
1917
|
+
* x: int
|
|
1918
|
+
*
|
|
1919
|
+
* @runtime_checkable
|
|
1920
|
+
* class HasY(Protocol):
|
|
1921
|
+
* y: int
|
|
1922
|
+
*
|
|
1923
|
+
* def f(arg: object, arg2: type):
|
|
1924
|
+
* isinstance(arg, HasX) # error: [isinstance-against-protocol] (not runtime-checkable)
|
|
1925
|
+
* issubclass(arg2, HasX) # error: [isinstance-against-protocol] (not runtime-checkable)
|
|
1926
|
+
*
|
|
1927
|
+
* def g(arg: object):
|
|
1928
|
+
* match arg:
|
|
1929
|
+
* case HasX(): # error: [isinstance-against-protocol] (not runtime-checkable)
|
|
1930
|
+
* pass
|
|
1931
|
+
*
|
|
1932
|
+
* def h(arg2: type):
|
|
1933
|
+
* isinstance(arg2, HasY) # fine (runtime-checkable)
|
|
1934
|
+
*
|
|
1935
|
+
* # `HasY` is runtime-checkable, but has non-method members,
|
|
1936
|
+
* # so it still can't be used in `issubclass` checks)
|
|
1937
|
+
* issubclass(arg2, HasY) # error: [isinstance-against-protocol]
|
|
1938
|
+
* ```
|
|
1939
|
+
*
|
|
1940
|
+
* ## References
|
|
1941
|
+
* - [Typing documentation: `@runtime_checkable`](https://docs.python.org/3/library/typing.html#typing.runtime_checkable)
|
|
1942
|
+
*/
|
|
1943
|
+
export type ReportsInvalidRuntimeChecksAgainstProtocolClasses = Level;
|
|
1944
|
+
/**
|
|
1945
|
+
* ## What it does
|
|
1946
|
+
* Reports runtime checks against `TypedDict` classes.
|
|
1947
|
+
* This includes explicit calls to `isinstance()`/`issubclass()` and implicit
|
|
1948
|
+
* checks performed by `match` class patterns.
|
|
1949
|
+
*
|
|
1950
|
+
* ## Why is this bad?
|
|
1951
|
+
* Using a `TypedDict` class in these contexts raises `TypeError` at runtime.
|
|
1952
|
+
*
|
|
1953
|
+
* ## Examples
|
|
1954
|
+
* ```python
|
|
1955
|
+
* from typing_extensions import TypedDict
|
|
1956
|
+
*
|
|
1957
|
+
* class Movie(TypedDict):
|
|
1958
|
+
* name: str
|
|
1959
|
+
* director: str
|
|
1960
|
+
*
|
|
1961
|
+
* def f(arg: object, arg2: type):
|
|
1962
|
+
* isinstance(arg, Movie) # error: [isinstance-against-typed-dict]
|
|
1963
|
+
* issubclass(arg2, Movie) # error: [isinstance-against-typed-dict]
|
|
1964
|
+
*
|
|
1965
|
+
* def g(arg: object):
|
|
1966
|
+
* match arg:
|
|
1967
|
+
* case Movie(): # error: [isinstance-against-typed-dict]
|
|
1968
|
+
* pass
|
|
1969
|
+
* ```
|
|
1970
|
+
*
|
|
1971
|
+
* ## References
|
|
1972
|
+
* - [Typing specification: `TypedDict`](https://typing.python.org/en/latest/spec/typeddict.html)
|
|
1973
|
+
*/
|
|
1974
|
+
export type ReportsRuntimeChecksAgainstTypedDictClasses = Level;
|
|
1975
|
+
/**
|
|
1976
|
+
* ## What it does
|
|
1977
|
+
* Checks for missing required arguments in a call.
|
|
1978
|
+
*
|
|
1979
|
+
* ## Why is this bad?
|
|
1980
|
+
* Failing to provide a required argument will raise a `TypeError` at runtime.
|
|
1981
|
+
*
|
|
1982
|
+
* ## Examples
|
|
1983
|
+
* ```python
|
|
1984
|
+
* def func(x: int): ...
|
|
1985
|
+
* func() # TypeError: func() missing 1 required positional argument: 'x'
|
|
1986
|
+
* ```
|
|
1987
|
+
*/
|
|
1988
|
+
export type DetectsMissingRequiredArgumentsInACall = Level;
|
|
1989
|
+
/**
|
|
1990
|
+
* ## What it does
|
|
1991
|
+
* Detects missing required keys in `TypedDict` constructor calls.
|
|
1992
|
+
*
|
|
1993
|
+
* ## Why is this bad?
|
|
1994
|
+
* `TypedDict` requires all non-optional keys to be provided during construction.
|
|
1995
|
+
* Missing items can lead to a `KeyError` at runtime.
|
|
1996
|
+
*
|
|
1997
|
+
* ## Example
|
|
1998
|
+
* ```python
|
|
1999
|
+
* from typing import TypedDict
|
|
2000
|
+
*
|
|
2001
|
+
* class Person(TypedDict):
|
|
2002
|
+
* name: str
|
|
2003
|
+
* age: int
|
|
2004
|
+
*
|
|
2005
|
+
* alice: Person = {"name": "Alice"} # missing required key 'age'
|
|
2006
|
+
*
|
|
2007
|
+
* alice["age"] # KeyError
|
|
2008
|
+
* ```
|
|
2009
|
+
*/
|
|
2010
|
+
export type DetectsMissingRequiredKeysInTypedDictConstructors = Level;
|
|
2011
|
+
/**
|
|
2012
|
+
* ## What it does
|
|
2013
|
+
* Checks for calls to an overloaded function that do not match any of the overloads.
|
|
2014
|
+
*
|
|
2015
|
+
* ## Why is this bad?
|
|
2016
|
+
* Failing to provide the correct arguments to one of the overloads will raise a `TypeError`
|
|
2017
|
+
* at runtime.
|
|
2018
|
+
*
|
|
2019
|
+
* ## Examples
|
|
2020
|
+
* ```python
|
|
2021
|
+
* @overload
|
|
2022
|
+
* def func(x: int): ...
|
|
2023
|
+
* @overload
|
|
2024
|
+
* def func(x: bool): ...
|
|
2025
|
+
* func("string") # error: [no-matching-overload]
|
|
2026
|
+
* ```
|
|
2027
|
+
*/
|
|
2028
|
+
export type DetectsCallsThatDoNotMatchAnyOverload = Level;
|
|
2029
|
+
/**
|
|
2030
|
+
* ## What it does
|
|
2031
|
+
* Checks for objects that are not iterable but are used in a context that requires them to be.
|
|
2032
|
+
*
|
|
2033
|
+
* ## Why is this bad?
|
|
2034
|
+
* Iterating over an object that is not iterable will raise a `TypeError` at runtime.
|
|
2035
|
+
*
|
|
2036
|
+
* ## Examples
|
|
2037
|
+
*
|
|
2038
|
+
* ```python
|
|
2039
|
+
* for i in 34: # TypeError: 'int' object is not iterable
|
|
2040
|
+
* pass
|
|
2041
|
+
* ```
|
|
2042
|
+
*/
|
|
2043
|
+
export type DetectsIterationOverAnObjectThatIsNotIterable = Level;
|
|
2044
|
+
/**
|
|
2045
|
+
* ## What it does
|
|
2046
|
+
* Checks for subscripting objects that do not support subscripting.
|
|
2047
|
+
*
|
|
2048
|
+
* ## Why is this bad?
|
|
2049
|
+
* Subscripting an object that does not support it will raise a `TypeError` at runtime.
|
|
2050
|
+
*
|
|
2051
|
+
* ## Examples
|
|
2052
|
+
* ```python
|
|
2053
|
+
* 4[1] # TypeError: 'int' object is not subscriptable
|
|
2054
|
+
* ```
|
|
2055
|
+
*/
|
|
2056
|
+
export type DetectsSubscriptingObjectsThatDoNotSupportSubscripting = Level;
|
|
2057
|
+
/**
|
|
2058
|
+
* ## What it does
|
|
2059
|
+
* Checks for methods on subclasses that override superclass methods decorated with `@final`.
|
|
2060
|
+
*
|
|
2061
|
+
* ## Why is this bad?
|
|
2062
|
+
* Decorating a method with `@final` declares to the type checker that it should not be
|
|
2063
|
+
* overridden on any subclass.
|
|
2064
|
+
*
|
|
2065
|
+
* ## Example
|
|
2066
|
+
*
|
|
2067
|
+
* ```python
|
|
2068
|
+
* from typing import final
|
|
2069
|
+
*
|
|
2070
|
+
* class A:
|
|
2071
|
+
* @final
|
|
2072
|
+
* def foo(self): ...
|
|
2073
|
+
*
|
|
2074
|
+
* class B(A):
|
|
2075
|
+
* def foo(self): ... # Error raised here
|
|
2076
|
+
* ```
|
|
2077
|
+
*/
|
|
2078
|
+
export type DetectsOverridesOfFinalMethods = Level;
|
|
2079
|
+
/**
|
|
2080
|
+
* ## What it does
|
|
2081
|
+
* Checks for class variables on subclasses that override a superclass variable
|
|
2082
|
+
* that has been declared as `Final`.
|
|
2083
|
+
*
|
|
2084
|
+
* ## Why is this bad?
|
|
2085
|
+
* Declaring a variable as `Final` indicates to the type checker that it should not be
|
|
2086
|
+
* overridden on any subclass.
|
|
2087
|
+
*
|
|
2088
|
+
* ## Example
|
|
2089
|
+
*
|
|
2090
|
+
* ```python
|
|
2091
|
+
* from typing import Final
|
|
2092
|
+
*
|
|
2093
|
+
* class A:
|
|
2094
|
+
* X: Final[int] = 1
|
|
2095
|
+
*
|
|
2096
|
+
* class B(A):
|
|
2097
|
+
* X = 2 # Error raised here
|
|
2098
|
+
* ```
|
|
2099
|
+
*/
|
|
2100
|
+
export type DetectsOverridesOfFinalClassVariables = Level;
|
|
2101
|
+
/**
|
|
2102
|
+
* ## What it does
|
|
2103
|
+
* Checks for calls which provide more than one argument for a single parameter.
|
|
2104
|
+
*
|
|
2105
|
+
* ## Why is this bad?
|
|
2106
|
+
* Providing multiple values for a single parameter will raise a `TypeError` at runtime.
|
|
2107
|
+
*
|
|
2108
|
+
* ## Examples
|
|
2109
|
+
*
|
|
2110
|
+
* ```python
|
|
2111
|
+
* def f(x: int) -> int: ...
|
|
2112
|
+
*
|
|
2113
|
+
* f(1, x=2) # Error raised here
|
|
2114
|
+
* ```
|
|
2115
|
+
*/
|
|
2116
|
+
export type DetectsMultipleArgumentsForTheSameParameter = Level;
|
|
2117
|
+
/**
|
|
2118
|
+
* ## What it does
|
|
2119
|
+
* Checks for keyword arguments in calls that match positional-only parameters of the callable.
|
|
2120
|
+
*
|
|
2121
|
+
* ## Why is this bad?
|
|
2122
|
+
* Providing a positional-only parameter as a keyword argument will raise `TypeError` at runtime.
|
|
2123
|
+
*
|
|
2124
|
+
* ## Example
|
|
2125
|
+
*
|
|
2126
|
+
* ```python
|
|
2127
|
+
* def f(x: int, /) -> int: ...
|
|
2128
|
+
*
|
|
2129
|
+
* f(x=1) # Error raised here
|
|
2130
|
+
* ```
|
|
2131
|
+
*/
|
|
2132
|
+
export type DetectsPositionalOnlyParametersPassedAsKeywordArguments = Level;
|
|
2133
|
+
/**
|
|
2134
|
+
* ## What it does
|
|
2135
|
+
* Checks for possibly missing attributes.
|
|
2136
|
+
*
|
|
2137
|
+
* ## Why is this bad?
|
|
2138
|
+
* Attempting to access a missing attribute will raise an `AttributeError` at runtime.
|
|
2139
|
+
*
|
|
2140
|
+
* ## Rule status
|
|
2141
|
+
* This rule is currently disabled by default because of the number of
|
|
2142
|
+
* false positives it can produce.
|
|
2143
|
+
*
|
|
2144
|
+
* ## Examples
|
|
2145
|
+
* ```python
|
|
2146
|
+
* class A:
|
|
2147
|
+
* if b:
|
|
2148
|
+
* c = 0
|
|
2149
|
+
*
|
|
2150
|
+
* A.c # AttributeError: type object 'A' has no attribute 'c'
|
|
2151
|
+
* ```
|
|
2152
|
+
*/
|
|
2153
|
+
export type DetectsReferencesToPossiblyMissingAttributes = Level;
|
|
2154
|
+
/**
|
|
2155
|
+
* ## What it does
|
|
2156
|
+
* Checks for implicit calls to possibly missing methods.
|
|
2157
|
+
*
|
|
2158
|
+
* ## Why is this bad?
|
|
2159
|
+
* Expressions such as `x[y]` and `x * y` call methods
|
|
2160
|
+
* under the hood (`__getitem__` and `__mul__` respectively).
|
|
2161
|
+
* Calling a missing method will raise an `AttributeError` at runtime.
|
|
2162
|
+
*
|
|
2163
|
+
* ## Examples
|
|
2164
|
+
* ```python
|
|
2165
|
+
* import datetime
|
|
2166
|
+
*
|
|
2167
|
+
* class A:
|
|
2168
|
+
* if datetime.date.today().weekday() != 6:
|
|
2169
|
+
* def __getitem__(self, v): ...
|
|
2170
|
+
*
|
|
2171
|
+
* A()[0] # TypeError: 'A' object is not subscriptable
|
|
2172
|
+
* ```
|
|
2173
|
+
*/
|
|
2174
|
+
export type DetectsImplicitCallsToPossiblyMissingMethods = Level;
|
|
2175
|
+
/**
|
|
2176
|
+
* ## What it does
|
|
2177
|
+
* Checks for imports of symbols that may be missing.
|
|
2178
|
+
*
|
|
2179
|
+
* ## Why is this bad?
|
|
2180
|
+
* Importing a missing module or name will raise a `ModuleNotFoundError`
|
|
2181
|
+
* or `ImportError` at runtime.
|
|
2182
|
+
*
|
|
2183
|
+
* ## Rule status
|
|
2184
|
+
* This rule is currently disabled by default because of the number of
|
|
2185
|
+
* false positives it can produce.
|
|
2186
|
+
*
|
|
2187
|
+
* ## Examples
|
|
2188
|
+
* ```python
|
|
2189
|
+
* # module.py
|
|
2190
|
+
* import datetime
|
|
2191
|
+
*
|
|
2192
|
+
* if datetime.date.today().weekday() != 6:
|
|
2193
|
+
* a = 1
|
|
2194
|
+
*
|
|
2195
|
+
* # main.py
|
|
2196
|
+
* from module import a # ImportError: cannot import name 'a' from 'module'
|
|
2197
|
+
* ```
|
|
2198
|
+
*/
|
|
2199
|
+
export type DetectsPossiblyMissingImports = Level;
|
|
2200
|
+
/**
|
|
2201
|
+
* ## What it does
|
|
2202
|
+
* Checks for accesses of submodules that might not've been imported.
|
|
2203
|
+
*
|
|
2204
|
+
* ## Why is this bad?
|
|
2205
|
+
* When module `a` has a submodule `b`, `import a` isn't generally enough to let you access
|
|
2206
|
+
* `a.b.` You either need to explicitly `import a.b`, or else you need the `__init__.py` file
|
|
2207
|
+
* of `a` to include `from . import b`. Without one of those, `a.b` is an `AttributeError`.
|
|
2208
|
+
*
|
|
2209
|
+
* ## Examples
|
|
2210
|
+
* ```python
|
|
2211
|
+
* import html
|
|
2212
|
+
* html.parser # AttributeError: module 'html' has no attribute 'parser'
|
|
2213
|
+
* ```
|
|
2214
|
+
*/
|
|
2215
|
+
export type DetectsAccessesOfSubmodulesThatMayNotBeAvailableAsAttributesOnTheirParentModule = Level;
|
|
2216
|
+
/**
|
|
2217
|
+
* ## What it does
|
|
2218
|
+
* Checks for references to names that are possibly not defined.
|
|
2219
|
+
*
|
|
2220
|
+
* ## Why is this bad?
|
|
2221
|
+
* Using an undefined variable will raise a `NameError` at runtime.
|
|
2222
|
+
*
|
|
2223
|
+
* ## Rule status
|
|
2224
|
+
* This rule is currently disabled by default because of the number of
|
|
2225
|
+
* false positives it can produce.
|
|
2226
|
+
*
|
|
2227
|
+
* ## Example
|
|
2228
|
+
*
|
|
2229
|
+
* ```python
|
|
2230
|
+
* for i in range(0):
|
|
2231
|
+
* x = i
|
|
2232
|
+
*
|
|
2233
|
+
* print(x) # NameError: name 'x' is not defined
|
|
2234
|
+
* ```
|
|
2235
|
+
*/
|
|
2236
|
+
export type DetectsReferencesToPossiblyUndefinedNames = Level;
|
|
2237
|
+
/**
|
|
2238
|
+
* ## What it does
|
|
2239
|
+
* Checks for raw-strings in type annotation positions.
|
|
2240
|
+
*
|
|
2241
|
+
* ## Why is this bad?
|
|
2242
|
+
* Static analysis tools like ty can't analyze type annotations that use raw-string notation.
|
|
2243
|
+
*
|
|
2244
|
+
* ## Examples
|
|
2245
|
+
* ```python
|
|
2246
|
+
* def test(): -> r"int":
|
|
2247
|
+
* ...
|
|
2248
|
+
* ```
|
|
2249
|
+
*
|
|
2250
|
+
* Use instead:
|
|
2251
|
+
* ```python
|
|
2252
|
+
* def test(): -> "int":
|
|
2253
|
+
* ...
|
|
2254
|
+
* ```
|
|
2255
|
+
*/
|
|
2256
|
+
export type DetectsRawStringsInTypeAnnotationPositions = Level;
|
|
2257
|
+
/**
|
|
2258
|
+
* ## What it does
|
|
2259
|
+
* Detects redundant `cast` calls where the value already has the target type.
|
|
2260
|
+
*
|
|
2261
|
+
* ## Why is this bad?
|
|
2262
|
+
* These casts have no effect and can be removed.
|
|
2263
|
+
*
|
|
2264
|
+
* ## Example
|
|
2265
|
+
* ```python
|
|
2266
|
+
* def f() -> int:
|
|
2267
|
+
* return 10
|
|
2268
|
+
*
|
|
2269
|
+
* cast(int, f()) # Redundant
|
|
2270
|
+
* ```
|
|
2271
|
+
*/
|
|
2272
|
+
export type DetectsRedundantCastCalls = Level;
|
|
2273
|
+
/**
|
|
2274
|
+
* ## What it does
|
|
2275
|
+
* Checks for redundant combinations of the `ClassVar` and `Final` type qualifiers.
|
|
2276
|
+
*
|
|
2277
|
+
* ## Why is this bad?
|
|
2278
|
+
* An attribute that is marked `Final` in a class body is implicitly a class variable.
|
|
2279
|
+
* Marking it as `ClassVar` is therefore redundant.
|
|
2280
|
+
*
|
|
2281
|
+
* Note that this diagnostic is not emitted for dataclass fields, where
|
|
2282
|
+
* `ClassVar[Final[int]]` has a distinct meaning from `Final[int]`.
|
|
2283
|
+
*
|
|
2284
|
+
* ## Examples
|
|
2285
|
+
* ```python
|
|
2286
|
+
* from typing import ClassVar, Final
|
|
2287
|
+
*
|
|
2288
|
+
* class C:
|
|
2289
|
+
* x: ClassVar[Final[int]] = 1 # redundant
|
|
2290
|
+
* y: Final[ClassVar[int]] = 1 # redundant
|
|
2291
|
+
* ```
|
|
2292
|
+
*/
|
|
2293
|
+
export type DetectsRedundantCombinationsOfClassVarAndFinal = Level;
|
|
2294
|
+
/**
|
|
2295
|
+
* ## What it does
|
|
2296
|
+
* Checks for type variables in nested generic classes or functions that shadow type variables
|
|
2297
|
+
* from an enclosing scope.
|
|
2298
|
+
*
|
|
2299
|
+
* ## Why is this bad?
|
|
2300
|
+
* Shadowing type variables makes the code confusing and is disallowed by the typing spec.
|
|
2301
|
+
*
|
|
2302
|
+
* ## Examples
|
|
2303
|
+
* ```python
|
|
2304
|
+
* class Outer[T]:
|
|
2305
|
+
* # Error: `T` is already used by `Outer`
|
|
2306
|
+
* class Inner[T]: ...
|
|
2307
|
+
*
|
|
2308
|
+
* # Error: `T` is already used by `Outer`
|
|
2309
|
+
* def method[T](self, x: T) -> T: ...
|
|
2310
|
+
* ```
|
|
2311
|
+
*
|
|
2312
|
+
* ## References
|
|
2313
|
+
* - [Typing spec: Generics](https://typing.python.org/en/latest/spec/generics.html#introduction)
|
|
2314
|
+
*/
|
|
2315
|
+
export type DetectsTypeVariablesThatShadowTypeVariablesFromOuterScopes = Level;
|
|
2316
|
+
/**
|
|
2317
|
+
* ## What it does
|
|
2318
|
+
* Makes sure that the argument of `static_assert` is statically known to be true.
|
|
2319
|
+
*
|
|
2320
|
+
* ## Why is this bad?
|
|
2321
|
+
* A `static_assert` call represents an explicit request from the user
|
|
2322
|
+
* for the type checker to emit an error if the argument cannot be verified
|
|
2323
|
+
* to evaluate to `True` in a boolean context.
|
|
2324
|
+
*
|
|
2325
|
+
* ## Examples
|
|
2326
|
+
* ```python
|
|
2327
|
+
* from ty_extensions import static_assert
|
|
2328
|
+
*
|
|
2329
|
+
* static_assert(1 + 1 == 3) # error: evaluates to `False`
|
|
2330
|
+
*
|
|
2331
|
+
* static_assert(int(2.0 * 3.0) == 6) # error: does not have a statically known truthiness
|
|
2332
|
+
* ```
|
|
2333
|
+
*/
|
|
2334
|
+
export type FailedStaticAssertion = Level;
|
|
2335
|
+
/**
|
|
2336
|
+
* ## What it does
|
|
2337
|
+
* Checks for classes that subclass final classes.
|
|
2338
|
+
*
|
|
2339
|
+
* ## Why is this bad?
|
|
2340
|
+
* Decorating a class with `@final` declares to the type checker that it should not be subclassed.
|
|
2341
|
+
*
|
|
2342
|
+
* ## Example
|
|
2343
|
+
*
|
|
2344
|
+
* ```python
|
|
2345
|
+
* from typing import final
|
|
2346
|
+
*
|
|
2347
|
+
* @final
|
|
2348
|
+
* class A: ...
|
|
2349
|
+
* class B(A): ... # Error raised here
|
|
2350
|
+
* ```
|
|
2351
|
+
*/
|
|
2352
|
+
export type DetectsSubclassesOfFinalClasses = Level;
|
|
2353
|
+
/**
|
|
2354
|
+
* ## What it does
|
|
2355
|
+
* Checks for calls to `super()` inside methods of `NamedTuple` classes.
|
|
2356
|
+
*
|
|
2357
|
+
* ## Why is this bad?
|
|
2358
|
+
* Using `super()` in a method of a `NamedTuple` class will raise an exception at runtime.
|
|
2359
|
+
*
|
|
2360
|
+
* ## Examples
|
|
2361
|
+
* ```python
|
|
2362
|
+
* from typing import NamedTuple
|
|
2363
|
+
*
|
|
2364
|
+
* class F(NamedTuple):
|
|
2365
|
+
* x: int
|
|
2366
|
+
*
|
|
2367
|
+
* def method(self):
|
|
2368
|
+
* super() # error: super() is not supported in methods of NamedTuple classes
|
|
2369
|
+
* ```
|
|
2370
|
+
*
|
|
2371
|
+
* ## References
|
|
2372
|
+
* - [Python documentation: super()](https://docs.python.org/3/library/functions.html#super)
|
|
2373
|
+
*/
|
|
2374
|
+
export type DetectsSuperCallsInMethodsOfNamedTupleClasses = Level;
|
|
2375
|
+
/**
|
|
2376
|
+
* ## What it does
|
|
2377
|
+
* Checks for calls that pass more positional arguments than the callable can accept.
|
|
2378
|
+
*
|
|
2379
|
+
* ## Why is this bad?
|
|
2380
|
+
* Passing too many positional arguments will raise `TypeError` at runtime.
|
|
2381
|
+
*
|
|
2382
|
+
* ## Example
|
|
2383
|
+
*
|
|
2384
|
+
* ```python
|
|
2385
|
+
* def f(): ...
|
|
2386
|
+
*
|
|
2387
|
+
* f("foo") # Error raised here
|
|
2388
|
+
* ```
|
|
2389
|
+
*/
|
|
2390
|
+
export type DetectsCallsPassingTooManyPositionalArguments = Level;
|
|
2391
|
+
/**
|
|
2392
|
+
* ## What it does
|
|
2393
|
+
* Checks for `assert_type()` and `assert_never()` calls where the actual type
|
|
2394
|
+
* is not the same as the asserted type.
|
|
2395
|
+
*
|
|
2396
|
+
* ## Why is this bad?
|
|
2397
|
+
* `assert_type()` allows confirming the inferred type of a certain value.
|
|
2398
|
+
*
|
|
2399
|
+
* ## Example
|
|
2400
|
+
*
|
|
2401
|
+
* ```python
|
|
2402
|
+
* def _(x: int):
|
|
2403
|
+
* assert_type(x, int) # fine
|
|
2404
|
+
* assert_type(x, str) # error: Actual type does not match asserted type
|
|
2405
|
+
* ```
|
|
2406
|
+
*/
|
|
2407
|
+
export type DetectsFailedTypeAssertions1 = Level;
|
|
2408
|
+
/**
|
|
2409
|
+
* ## What it does
|
|
2410
|
+
* Detects invalid `super()` calls where implicit arguments like the enclosing class or first method argument are unavailable.
|
|
2411
|
+
*
|
|
2412
|
+
* ## Why is this bad?
|
|
2413
|
+
* When `super()` is used without arguments, Python tries to find two things:
|
|
2414
|
+
* the nearest enclosing class and the first argument of the immediately enclosing function (typically self or cls).
|
|
2415
|
+
* If either of these is missing, the call will fail at runtime with a `RuntimeError`.
|
|
2416
|
+
*
|
|
2417
|
+
* ## Examples
|
|
2418
|
+
* ```python
|
|
2419
|
+
* super() # error: no enclosing class or function found
|
|
2420
|
+
*
|
|
2421
|
+
* def func():
|
|
2422
|
+
* super() # error: no enclosing class or first argument exists
|
|
2423
|
+
*
|
|
2424
|
+
* class A:
|
|
2425
|
+
* f = super() # error: no enclosing function to provide the first argument
|
|
2426
|
+
*
|
|
2427
|
+
* def method(self):
|
|
2428
|
+
* def nested():
|
|
2429
|
+
* super() # error: first argument does not exist in this nested function
|
|
2430
|
+
*
|
|
2431
|
+
* lambda: super() # error: first argument does not exist in this lambda
|
|
2432
|
+
*
|
|
2433
|
+
* (super() for _ in range(10)) # error: argument is not available in generator expression
|
|
2434
|
+
*
|
|
2435
|
+
* super() # okay! both enclosing class and first argument are available
|
|
2436
|
+
* ```
|
|
2437
|
+
*
|
|
2438
|
+
* ## References
|
|
2439
|
+
* - [Python documentation: super()](https://docs.python.org/3/library/functions.html#super)
|
|
2440
|
+
*/
|
|
2441
|
+
export type DetectsInvalidSuperCallsWhereImplicitArgumentsAreUnavailable = Level;
|
|
2442
|
+
/**
|
|
2443
|
+
* ## What it does
|
|
2444
|
+
* Checks for type variables that are used in a scope where they are not bound
|
|
2445
|
+
* to any enclosing generic context.
|
|
2446
|
+
*
|
|
2447
|
+
* ## Why is this bad?
|
|
2448
|
+
* Using a type variable outside of a scope that binds it has no well-defined meaning.
|
|
2449
|
+
*
|
|
2450
|
+
* ## Examples
|
|
2451
|
+
* ```python
|
|
2452
|
+
* from typing import TypeVar, Generic
|
|
2453
|
+
*
|
|
2454
|
+
* T = TypeVar("T")
|
|
2455
|
+
* S = TypeVar("S")
|
|
2456
|
+
*
|
|
2457
|
+
* x: T # error: unbound type variable in module scope
|
|
2458
|
+
*
|
|
2459
|
+
* class C(Generic[T]):
|
|
2460
|
+
* x: list[S] = [] # error: S is not in this class's generic context
|
|
2461
|
+
* ```
|
|
2462
|
+
*
|
|
2463
|
+
* ## References
|
|
2464
|
+
* - [Typing spec: Scoping rules for type variables](https://typing.python.org/en/latest/spec/generics.html#scoping-rules-for-type-variables)
|
|
2465
|
+
*/
|
|
2466
|
+
export type DetectsTypeVariablesUsedOutsideOfTheirBoundScope = Level;
|
|
2467
|
+
/**
|
|
2468
|
+
* ## What it does
|
|
2469
|
+
* Checks for calls to `reveal_type` without importing it.
|
|
2470
|
+
*
|
|
2471
|
+
* ## Why is this bad?
|
|
2472
|
+
* Using `reveal_type` without importing it will raise a `NameError` at runtime.
|
|
2473
|
+
*
|
|
2474
|
+
* ## Examples
|
|
2475
|
+
* ```python
|
|
2476
|
+
* reveal_type(1) # NameError: name 'reveal_type' is not defined
|
|
2477
|
+
* ```
|
|
2478
|
+
*/
|
|
2479
|
+
export type DetectsUsagesOfRevealTypeWithoutImportingIt = Level;
|
|
2480
|
+
/**
|
|
2481
|
+
* ## What it does
|
|
2482
|
+
* Checks for keyword arguments in calls that don't match any parameter of the callable.
|
|
2483
|
+
*
|
|
2484
|
+
* ## Why is this bad?
|
|
2485
|
+
* Providing an unknown keyword argument will raise `TypeError` at runtime.
|
|
2486
|
+
*
|
|
2487
|
+
* ## Example
|
|
2488
|
+
*
|
|
2489
|
+
* ```python
|
|
2490
|
+
* def f(x: int) -> int: ...
|
|
2491
|
+
*
|
|
2492
|
+
* f(x=1, y=2) # Error raised here
|
|
2493
|
+
* ```
|
|
2494
|
+
*/
|
|
2495
|
+
export type DetectsUnknownKeywordArgumentsInCalls = Level;
|
|
2496
|
+
/**
|
|
2497
|
+
* ## What it does
|
|
2498
|
+
* Checks for unresolved attributes.
|
|
2499
|
+
*
|
|
2500
|
+
* ## Why is this bad?
|
|
2501
|
+
* Accessing an unbound attribute will raise an `AttributeError` at runtime.
|
|
2502
|
+
* An unresolved attribute is not guaranteed to exist from the type alone,
|
|
2503
|
+
* so this could also indicate that the object is not of the type that the user expects.
|
|
2504
|
+
*
|
|
2505
|
+
* ## Examples
|
|
2506
|
+
* ```python
|
|
2507
|
+
* class A: ...
|
|
2508
|
+
*
|
|
2509
|
+
* A().foo # AttributeError: 'A' object has no attribute 'foo'
|
|
2510
|
+
* ```
|
|
2511
|
+
*/
|
|
2512
|
+
export type DetectsReferencesToUnresolvedAttributes = Level;
|
|
2513
|
+
/**
|
|
2514
|
+
* ## What it does
|
|
2515
|
+
* Detects variables declared as `global` in an inner scope that have no explicit
|
|
2516
|
+
* bindings or declarations in the global scope.
|
|
2517
|
+
*
|
|
2518
|
+
* ## Why is this bad?
|
|
2519
|
+
* Function bodies with `global` statements can run in any order (or not at all), which makes
|
|
2520
|
+
* it hard for static analysis tools to infer the types of globals without
|
|
2521
|
+
* explicit definitions or declarations.
|
|
2522
|
+
*
|
|
2523
|
+
* ## Example
|
|
2524
|
+
* ```python
|
|
2525
|
+
* def f():
|
|
2526
|
+
* global x # unresolved global
|
|
2527
|
+
* x = 42
|
|
2528
|
+
*
|
|
2529
|
+
* def g():
|
|
2530
|
+
* print(x) # unresolved reference
|
|
2531
|
+
* ```
|
|
2532
|
+
*
|
|
2533
|
+
* Use instead:
|
|
2534
|
+
*
|
|
2535
|
+
* ```python
|
|
2536
|
+
* x: int
|
|
2537
|
+
*
|
|
2538
|
+
* def f():
|
|
2539
|
+
* global x
|
|
2540
|
+
* x = 42
|
|
2541
|
+
*
|
|
2542
|
+
* def g():
|
|
2543
|
+
* print(x)
|
|
2544
|
+
* ```
|
|
2545
|
+
*
|
|
2546
|
+
* Or:
|
|
2547
|
+
*
|
|
2548
|
+
* ```python
|
|
2549
|
+
* x: int | None = None
|
|
2550
|
+
*
|
|
2551
|
+
* def f():
|
|
2552
|
+
* global x
|
|
2553
|
+
* x = 42
|
|
2554
|
+
*
|
|
2555
|
+
* def g():
|
|
2556
|
+
* print(x)
|
|
2557
|
+
* ```
|
|
2558
|
+
*/
|
|
2559
|
+
export type DetectsGlobalStatementsWithNoDefinitionInTheGlobalScope = Level;
|
|
2560
|
+
/**
|
|
2561
|
+
* ## What it does
|
|
2562
|
+
* Checks for import statements for which the module cannot be resolved.
|
|
2563
|
+
*
|
|
2564
|
+
* ## Why is this bad?
|
|
2565
|
+
* Importing a module that cannot be resolved will raise a `ModuleNotFoundError`
|
|
2566
|
+
* at runtime.
|
|
2567
|
+
*
|
|
2568
|
+
* ## Examples
|
|
2569
|
+
* ```python
|
|
2570
|
+
* import foo # ModuleNotFoundError: No module named 'foo'
|
|
2571
|
+
* ```
|
|
2572
|
+
*/
|
|
2573
|
+
export type DetectsUnresolvedImports = Level;
|
|
2574
|
+
/**
|
|
2575
|
+
* ## What it does
|
|
2576
|
+
* Checks for references to names that are not defined.
|
|
2577
|
+
*
|
|
2578
|
+
* ## Why is this bad?
|
|
2579
|
+
* Using an undefined variable will raise a `NameError` at runtime.
|
|
2580
|
+
*
|
|
2581
|
+
* ## Example
|
|
2582
|
+
*
|
|
2583
|
+
* ```python
|
|
2584
|
+
* print(x) # NameError: name 'x' is not defined
|
|
2585
|
+
* ```
|
|
2586
|
+
*/
|
|
2587
|
+
export type DetectsReferencesToNamesThatAreNotDefined = Level;
|
|
2588
|
+
/**
|
|
2589
|
+
* ## What it does
|
|
2590
|
+
* Checks for class definitions that have bases which are unsupported by ty.
|
|
2591
|
+
*
|
|
2592
|
+
* ## Why is this bad?
|
|
2593
|
+
* If a class has a base that is an instance of a complex type such as a union type,
|
|
2594
|
+
* ty will not be able to resolve the [method resolution order] (MRO) for the class.
|
|
2595
|
+
* This will lead to an inferior understanding of your codebase and unpredictable
|
|
2596
|
+
* type-checking behavior.
|
|
2597
|
+
*
|
|
2598
|
+
* ## Examples
|
|
2599
|
+
* ```python
|
|
2600
|
+
* import datetime
|
|
2601
|
+
*
|
|
2602
|
+
* class A: ...
|
|
2603
|
+
* class B: ...
|
|
2604
|
+
*
|
|
2605
|
+
* if datetime.date.today().weekday() != 6:
|
|
2606
|
+
* C = A
|
|
2607
|
+
* else:
|
|
2608
|
+
* C = B
|
|
2609
|
+
*
|
|
2610
|
+
* class D(C): ... # error: [unsupported-base]
|
|
2611
|
+
* ```
|
|
2612
|
+
*
|
|
2613
|
+
* [method resolution order]: https://docs.python.org/3/glossary.html#term-method-resolution-order
|
|
2614
|
+
*/
|
|
2615
|
+
export type DetectsClassBasesThatAreUnsupportedAsTyCouldNotFeasiblyCalculateTheClassSMRO = Level;
|
|
2616
|
+
/**
|
|
2617
|
+
* ## What it does
|
|
2618
|
+
* Checks for bool conversions where the object doesn't correctly implement `__bool__`.
|
|
2619
|
+
*
|
|
2620
|
+
* ## Why is this bad?
|
|
2621
|
+
* If an exception is raised when you attempt to evaluate the truthiness of an object,
|
|
2622
|
+
* using the object in a boolean context will fail at runtime.
|
|
2623
|
+
*
|
|
2624
|
+
* ## Examples
|
|
2625
|
+
*
|
|
2626
|
+
* ```python
|
|
2627
|
+
* class NotBoolable:
|
|
2628
|
+
* __bool__ = None
|
|
2629
|
+
*
|
|
2630
|
+
* b1 = NotBoolable()
|
|
2631
|
+
* b2 = NotBoolable()
|
|
2632
|
+
*
|
|
2633
|
+
* if b1: # exception raised here
|
|
2634
|
+
* pass
|
|
2635
|
+
*
|
|
2636
|
+
* b1 and b2 # exception raised here
|
|
2637
|
+
* not b1 # exception raised here
|
|
2638
|
+
* b1 < b2 < b1 # exception raised here
|
|
2639
|
+
* ```
|
|
2640
|
+
*/
|
|
2641
|
+
export type DetectsBooleanConversionWhereTheObjectIncorrectlyImplements_Bool__ = Level;
|
|
2642
|
+
/**
|
|
2643
|
+
* ## What it does
|
|
2644
|
+
* Checks for dynamic class definitions (using `type()`) that have bases
|
|
2645
|
+
* which are unsupported by ty.
|
|
2646
|
+
*
|
|
2647
|
+
* This is equivalent to `unsupported-base` but applies to classes created
|
|
2648
|
+
* via `type()` rather than `class` statements.
|
|
2649
|
+
*
|
|
2650
|
+
* ## Why is this bad?
|
|
2651
|
+
* If a dynamically created class has a base that is an unsupported type
|
|
2652
|
+
* such as `type[T]`, ty will not be able to resolve the
|
|
2653
|
+
* [method resolution order] (MRO) for the class. This may lead to an inferior
|
|
2654
|
+
* understanding of your codebase and unpredictable type-checking behavior.
|
|
2655
|
+
*
|
|
2656
|
+
* ## Default level
|
|
2657
|
+
* This rule is disabled by default because it will not cause a runtime error,
|
|
2658
|
+
* and may be noisy on codebases that use `type()` in highly dynamic ways.
|
|
2659
|
+
*
|
|
2660
|
+
* ## Examples
|
|
2661
|
+
* ```python
|
|
2662
|
+
* def factory(base: type[Base]) -> type:
|
|
2663
|
+
* # `base` has type `type[Base]`, not `type[Base]` itself
|
|
2664
|
+
* return type("Dynamic", (base,), {}) # error: [unsupported-dynamic-base]
|
|
2665
|
+
* ```
|
|
2666
|
+
*
|
|
2667
|
+
* [method resolution order]: https://docs.python.org/3/glossary.html#term-method-resolution-order
|
|
2668
|
+
*/
|
|
2669
|
+
export type DetectsDynamicClassBasesThatAreUnsupportedAsTyCouldNotFeasiblyCalculateTheClassSMRO = Level;
|
|
2670
|
+
/**
|
|
2671
|
+
* ## What it does
|
|
2672
|
+
* Checks for binary expressions, comparisons, and unary expressions where
|
|
2673
|
+
* the operands don't support the operator.
|
|
2674
|
+
*
|
|
2675
|
+
* ## Why is this bad?
|
|
2676
|
+
* Attempting to use an unsupported operator will raise a `TypeError` at
|
|
2677
|
+
* runtime.
|
|
2678
|
+
*
|
|
2679
|
+
* ## Examples
|
|
2680
|
+
* ```python
|
|
2681
|
+
* class A: ...
|
|
2682
|
+
*
|
|
2683
|
+
* A() + A() # TypeError: unsupported operand type(s) for +: 'A' and 'A'
|
|
2684
|
+
* ```
|
|
2685
|
+
*/
|
|
2686
|
+
export type DetectsBinaryUnaryOrComparisonExpressionsWhereTheOperandsDonTSupportTheOperator = Level;
|
|
2687
|
+
/**
|
|
2688
|
+
* ## What it does
|
|
2689
|
+
* Checks for awaitable objects (such as coroutines) used as expression
|
|
2690
|
+
* statements without being awaited.
|
|
2691
|
+
*
|
|
2692
|
+
* ## Why is this bad?
|
|
2693
|
+
* Calling an `async def` function returns a coroutine object. If the
|
|
2694
|
+
* coroutine is never awaited, the body of the async function will never
|
|
2695
|
+
* execute, which is almost always a bug. Python emits a
|
|
2696
|
+
* `RuntimeWarning: coroutine was never awaited` at runtime in this case.
|
|
2697
|
+
*
|
|
2698
|
+
* ## Examples
|
|
2699
|
+
* ```python
|
|
2700
|
+
* async def fetch_data() -> str:
|
|
2701
|
+
* return "data"
|
|
2702
|
+
*
|
|
2703
|
+
* async def main() -> None:
|
|
2704
|
+
* fetch_data() # Warning: coroutine is not awaited
|
|
2705
|
+
* await fetch_data() # OK
|
|
2706
|
+
* ```
|
|
2707
|
+
*/
|
|
2708
|
+
export type DetectsAwaitableObjectsThatAreUsedAsExpressionStatementsWithoutBeingAwaited = Level;
|
|
2709
|
+
/**
|
|
2710
|
+
* ## What it does
|
|
2711
|
+
* Checks for `ty: ignore` directives that are no longer applicable.
|
|
2712
|
+
*
|
|
2713
|
+
* ## Why is this bad?
|
|
2714
|
+
* A `ty: ignore` directive that no longer matches any diagnostic violations is likely
|
|
2715
|
+
* included by mistake, and should be removed to avoid confusion.
|
|
2716
|
+
*
|
|
2717
|
+
* ## Examples
|
|
2718
|
+
* ```py
|
|
2719
|
+
* a = 20 / 2 # ty: ignore[division-by-zero]
|
|
2720
|
+
* ```
|
|
2721
|
+
*
|
|
2722
|
+
* Use instead:
|
|
2723
|
+
*
|
|
2724
|
+
* ```py
|
|
2725
|
+
* a = 20 / 2
|
|
2726
|
+
* ```
|
|
2727
|
+
*
|
|
2728
|
+
* ## Options
|
|
2729
|
+
* Set [`analysis.respect-type-ignore-comments`](https://docs.astral.sh/ty/reference/configuration/#respect-type-ignore-comments)
|
|
2730
|
+
* to `false` to prevent this rule from reporting unused `type: ignore` comments.
|
|
2731
|
+
*/
|
|
2732
|
+
export type DetectsUnusedTyIgnoreComments = Level;
|
|
2733
|
+
/**
|
|
2734
|
+
* ## What it does
|
|
2735
|
+
* Checks for `type: ignore` directives that are no longer applicable.
|
|
2736
|
+
*
|
|
2737
|
+
* ## Why is this bad?
|
|
2738
|
+
* A `type: ignore` directive that no longer matches any diagnostic violations is likely
|
|
2739
|
+
* included by mistake, and should be removed to avoid confusion.
|
|
2740
|
+
*
|
|
2741
|
+
* ## Examples
|
|
2742
|
+
* ```py
|
|
2743
|
+
* a = 20 / 2 # type: ignore
|
|
2744
|
+
* ```
|
|
2745
|
+
*
|
|
2746
|
+
* Use instead:
|
|
2747
|
+
*
|
|
2748
|
+
* ```py
|
|
2749
|
+
* a = 20 / 2
|
|
2750
|
+
* ```
|
|
2751
|
+
*
|
|
2752
|
+
* ## Options
|
|
2753
|
+
*
|
|
2754
|
+
* This rule is skipped if [`analysis.respect-type-ignore-comments`](https://docs.astral.sh/ty/reference/configuration/#respect-type-ignore-comments)
|
|
2755
|
+
* to `false`.
|
|
2756
|
+
*/
|
|
2757
|
+
export type DetectsUnusedTypeIgnoreComments = Level;
|
|
2758
|
+
/**
|
|
2759
|
+
* ## What it does
|
|
2760
|
+
* Checks for various `@overload`-decorated functions that have non-stub bodies.
|
|
2761
|
+
*
|
|
2762
|
+
* ## Why is this bad?
|
|
2763
|
+
* Functions decorated with `@overload` are ignored at runtime; they are overridden
|
|
2764
|
+
* by the implementation function that follows the series of overloads. While it is
|
|
2765
|
+
* not illegal to provide a body for an `@overload`-decorated function, it may indicate
|
|
2766
|
+
* a misunderstanding of how the `@overload` decorator works.
|
|
2767
|
+
*
|
|
2768
|
+
* ## Example
|
|
2769
|
+
*
|
|
2770
|
+
* ```py
|
|
2771
|
+
* from typing import overload
|
|
2772
|
+
*
|
|
2773
|
+
* @overload
|
|
2774
|
+
* def foo(x: int) -> int:
|
|
2775
|
+
* return x + 1 # will never be executed
|
|
2776
|
+
*
|
|
2777
|
+
* @overload
|
|
2778
|
+
* def foo(x: str) -> str:
|
|
2779
|
+
* return "Oh no, got a string" # will never be executed
|
|
2780
|
+
*
|
|
2781
|
+
* def foo(x: int | str) -> int | str:
|
|
2782
|
+
* raise Exception("unexpected type encountered")
|
|
2783
|
+
* ```
|
|
2784
|
+
*
|
|
2785
|
+
* Use instead:
|
|
2786
|
+
*
|
|
2787
|
+
* ```py
|
|
2788
|
+
* from typing import assert_never, overload
|
|
2789
|
+
*
|
|
2790
|
+
* @overload
|
|
2791
|
+
* def foo(x: int) -> int: ...
|
|
2792
|
+
*
|
|
2793
|
+
* @overload
|
|
2794
|
+
* def foo(x: str) -> str: ...
|
|
2795
|
+
*
|
|
2796
|
+
* def foo(x: int | str) -> int | str:
|
|
2797
|
+
* if isinstance(x, int):
|
|
2798
|
+
* return x + 1
|
|
2799
|
+
* elif isinstance(x, str):
|
|
2800
|
+
* return "Oh no, got a string"
|
|
2801
|
+
* else:
|
|
2802
|
+
* assert_never(x)
|
|
2803
|
+
* ```
|
|
2804
|
+
*
|
|
2805
|
+
* ## References
|
|
2806
|
+
* - [Python documentation: `@overload`](https://docs.python.org/3/library/typing.html#typing.overload)
|
|
2807
|
+
*/
|
|
2808
|
+
export type DetectsOverloadDecoratedFunctionsWithNonStubBodies = Level;
|
|
2809
|
+
/**
|
|
2810
|
+
* ## What it does
|
|
2811
|
+
* Checks for step size 0 in slices.
|
|
2812
|
+
*
|
|
2813
|
+
* ## Why is this bad?
|
|
2814
|
+
* A slice with a step size of zero will raise a `ValueError` at runtime.
|
|
2815
|
+
*
|
|
2816
|
+
* ## Examples
|
|
2817
|
+
* ```python
|
|
2818
|
+
* l = list(range(10))
|
|
2819
|
+
* l[1:10:0] # ValueError: slice step cannot be zero
|
|
2820
|
+
* ```
|
|
2821
|
+
*/
|
|
2822
|
+
export type DetectsASliceStepSizeOfZero = Level;
|
|
2823
|
+
/**
|
|
2824
|
+
* Configuration override that applies to specific files based on glob patterns.
|
|
2825
|
+
*
|
|
2826
|
+
* An override allows you to apply different rule configurations to specific
|
|
2827
|
+
* files or directories. Multiple overrides can match the same file, with
|
|
2828
|
+
* later overrides take precedence. Override rules take precedence over global
|
|
2829
|
+
* rules for matching files.
|
|
2830
|
+
*
|
|
2831
|
+
* For example, to relax enforcement of rules in test files:
|
|
2832
|
+
*
|
|
2833
|
+
* ```toml
|
|
2834
|
+
* [[tool.ty.overrides]]
|
|
2835
|
+
* include = ["tests/**", "** /test_*.py"]
|
|
2836
|
+
*
|
|
2837
|
+
* [tool.ty.overrides.rules]
|
|
2838
|
+
* possibly-unresolved-reference = "warn"
|
|
2839
|
+
* ```
|
|
2840
|
+
*
|
|
2841
|
+
* Or, to ignore a rule in generated files but retain enforcement in an important file:
|
|
2842
|
+
*
|
|
2843
|
+
* ```toml
|
|
2844
|
+
* [[tool.ty.overrides]]
|
|
2845
|
+
* include = ["generated/**"]
|
|
2846
|
+
* exclude = ["generated/important.py"]
|
|
2847
|
+
*
|
|
2848
|
+
* [tool.ty.overrides.rules]
|
|
2849
|
+
* possibly-unresolved-reference = "ignore"
|
|
2850
|
+
* ```
|
|
2851
|
+
*/
|
|
2852
|
+
export type OverridesOptions = OverrideOptions[];
|
|
2853
|
+
/**
|
|
2854
|
+
* The diagnostic output format.
|
|
2855
|
+
*/
|
|
2856
|
+
export type OutputFormat = 'full' | 'concise' | 'gitlab' | 'github' | 'junit';
|
|
2857
|
+
|
|
2858
|
+
export interface Options {
|
|
2859
|
+
analysis?: AnalysisOptions | null;
|
|
2860
|
+
/**
|
|
2861
|
+
* Configures the type checking environment.
|
|
2862
|
+
*/
|
|
2863
|
+
environment?: EnvironmentOptions | null;
|
|
2864
|
+
/**
|
|
2865
|
+
* Override configurations for specific file patterns.
|
|
2866
|
+
*
|
|
2867
|
+
* Each override specifies include/exclude patterns and rule configurations
|
|
2868
|
+
* that apply to matching files. Multiple overrides can match the same file,
|
|
2869
|
+
* with later overrides taking precedence.
|
|
2870
|
+
*/
|
|
2871
|
+
overrides?: OverridesOptions | null;
|
|
2872
|
+
/**
|
|
2873
|
+
* Configures the enabled rules and their severity.
|
|
2874
|
+
*
|
|
2875
|
+
* The keys are either rule names or `all` to set a default severity for all rules.
|
|
2876
|
+
* See [the rules documentation](https://ty.dev/rules) for a list of all available rules.
|
|
2877
|
+
*
|
|
2878
|
+
* Valid severities are:
|
|
2879
|
+
*
|
|
2880
|
+
* * `ignore`: Disable the rule.
|
|
2881
|
+
* * `warn`: Enable the rule and create a warning diagnostic.
|
|
2882
|
+
* * `error`: Enable the rule and create an error diagnostic.
|
|
2883
|
+
* ty will exit with a non-zero code if any error diagnostics are emitted.
|
|
2884
|
+
*/
|
|
2885
|
+
rules?: Rules | null;
|
|
2886
|
+
src?: SrcOptions | null;
|
|
2887
|
+
terminal?: TerminalOptions | null;
|
|
2888
|
+
}
|
|
2889
|
+
export interface AnalysisOptions {
|
|
2890
|
+
/**
|
|
2891
|
+
* A list of module glob patterns for which `unresolved-import` diagnostics should be suppressed.
|
|
2892
|
+
*
|
|
2893
|
+
* Details on supported glob patterns:
|
|
2894
|
+
* - `*` matches zero or more characters except `.`. For example, `foo.*` matches `foo.bar` but
|
|
2895
|
+
* not `foo.bar.baz`; `foo*` matches `foo` and `foobar` but not `foo.bar` or `barfoo`; and `*foo`
|
|
2896
|
+
* matches `foo` and `barfoo` but not `foo.bar` or `foobar`.
|
|
2897
|
+
* - `**` matches any number of module components (e.g., `foo.**` matches `foo`, `foo.bar`, etc.)
|
|
2898
|
+
* - Prefix a pattern with `!` to exclude matching modules
|
|
2899
|
+
*
|
|
2900
|
+
* When multiple patterns match, later entries take precedence.
|
|
2901
|
+
*
|
|
2902
|
+
* Glob patterns can be used in combinations with each other. For example, to suppress errors for
|
|
2903
|
+
* any module where the first component contains the substring `test`, use `*test*.**`.
|
|
2904
|
+
*/
|
|
2905
|
+
'allowed-unresolved-imports'?: String[] | null;
|
|
2906
|
+
/**
|
|
2907
|
+
* A list of module glob patterns whose imports should be replaced with `typing.Any`.
|
|
2908
|
+
*
|
|
2909
|
+
* Unlike `allowed-unresolved-imports`, this setting replaces the module's type information
|
|
2910
|
+
* with `typing.Any` even if the module can be resolved. Import diagnostics are
|
|
2911
|
+
* unconditionally suppressed for matching modules.
|
|
2912
|
+
*
|
|
2913
|
+
* - Prefix a pattern with `!` to exclude matching modules
|
|
2914
|
+
*
|
|
2915
|
+
* When multiple patterns match, later entries take precedence.
|
|
2916
|
+
*
|
|
2917
|
+
* Glob patterns can be used in combinations with each other. For example, to suppress errors for
|
|
2918
|
+
* any module where the first component contains the substring `test`, use `*test*.**`.
|
|
2919
|
+
*
|
|
2920
|
+
* When multiple patterns match, later entries take precedence.
|
|
2921
|
+
*/
|
|
2922
|
+
'replace-imports-with-any'?: String[] | null;
|
|
2923
|
+
/**
|
|
2924
|
+
* Whether ty should respect `type: ignore` comments.
|
|
2925
|
+
*
|
|
2926
|
+
* When set to `false`, `type: ignore` comments are treated like any other normal
|
|
2927
|
+
* comment and can't be used to suppress ty errors (you have to use `ty: ignore` instead).
|
|
2928
|
+
*
|
|
2929
|
+
* Setting this option can be useful when using ty alongside other type checkers or when
|
|
2930
|
+
* you prefer using `ty: ignore` over `type: ignore`.
|
|
2931
|
+
*
|
|
2932
|
+
* Defaults to `true`.
|
|
2933
|
+
*/
|
|
2934
|
+
'respect-type-ignore-comments'?: boolean | null;
|
|
2935
|
+
}
|
|
2936
|
+
export interface EnvironmentOptions {
|
|
2937
|
+
/**
|
|
2938
|
+
* User-provided paths that should take first priority in module resolution.
|
|
2939
|
+
*
|
|
2940
|
+
* This is an advanced option that should usually only be used for first-party or third-party
|
|
2941
|
+
* modules that are not installed into your Python environment in a conventional way.
|
|
2942
|
+
* Use the `python` option to specify the location of your Python environment.
|
|
2943
|
+
*
|
|
2944
|
+
* This option is similar to mypy's `MYPYPATH` environment variable and pyright's `stubPath`
|
|
2945
|
+
* configuration setting.
|
|
2946
|
+
*/
|
|
2947
|
+
'extra-paths'?: RelativePathBuf[] | null;
|
|
2948
|
+
/**
|
|
2949
|
+
* Path to your project's Python environment or interpreter.
|
|
2950
|
+
*
|
|
2951
|
+
* ty uses the `site-packages` directory of your project's Python environment
|
|
2952
|
+
* to resolve third-party (and, in some cases, first-party) imports in your code.
|
|
2953
|
+
*
|
|
2954
|
+
* This can be a path to:
|
|
2955
|
+
*
|
|
2956
|
+
* - A Python interpreter, e.g. `.venv/bin/python3`
|
|
2957
|
+
* - A virtual environment directory, e.g. `.venv`
|
|
2958
|
+
* - A system Python [`sys.prefix`] directory, e.g. `/usr`
|
|
2959
|
+
*
|
|
2960
|
+
* If you're using a project management tool such as uv, you should not generally need to
|
|
2961
|
+
* specify this option, as commands such as `uv run` will set the `VIRTUAL_ENV` environment
|
|
2962
|
+
* variable to point to your project's virtual environment. ty can also infer the location of
|
|
2963
|
+
* your environment from an activated Conda environment, and will look for a `.venv` directory
|
|
2964
|
+
* in the project root if none of the above apply. Failing that, ty will look for a `python3`
|
|
2965
|
+
* or `python` binary available in `PATH`.
|
|
2966
|
+
*
|
|
2967
|
+
* [`sys.prefix`]: https://docs.python.org/3/library/sys.html#sys.prefix
|
|
2968
|
+
*/
|
|
2969
|
+
python?: RelativePathBuf | null;
|
|
2970
|
+
/**
|
|
2971
|
+
* Specifies the target platform that will be used to analyze the source code.
|
|
2972
|
+
* If specified, ty will understand conditions based on comparisons with `sys.platform`, such
|
|
2973
|
+
* as are commonly found in typeshed to reflect the differing contents of the standard library across platforms.
|
|
2974
|
+
* If `all` is specified, ty will assume that the source code can run on any platform.
|
|
2975
|
+
*
|
|
2976
|
+
* If no platform is specified, ty will use the current platform:
|
|
2977
|
+
* - `win32` for Windows
|
|
2978
|
+
* - `darwin` for macOS
|
|
2979
|
+
* - `android` for Android
|
|
2980
|
+
* - `ios` for iOS
|
|
2981
|
+
* - `linux` for everything else
|
|
2982
|
+
*/
|
|
2983
|
+
'python-platform'?: PythonPlatform | null;
|
|
2984
|
+
/**
|
|
2985
|
+
* Specifies the version of Python that will be used to analyze the source code.
|
|
2986
|
+
* The version should be specified as a string in the format `M.m` where `M` is the major version
|
|
2987
|
+
* and `m` is the minor (e.g. `"3.0"` or `"3.6"`).
|
|
2988
|
+
* If a version is provided, ty will generate errors if the source code makes use of language features
|
|
2989
|
+
* that are not supported in that version.
|
|
2990
|
+
*
|
|
2991
|
+
* If a version is not specified, ty will try the following techniques in order of preference
|
|
2992
|
+
* to determine a value:
|
|
2993
|
+
* 1. Check for the `project.requires-python` setting in a `pyproject.toml` file
|
|
2994
|
+
* and use the minimum version from the specified range
|
|
2995
|
+
* 2. Check for an activated or configured Python environment
|
|
2996
|
+
* and attempt to infer the Python version of that environment
|
|
2997
|
+
* 3. Fall back to the default value (see below)
|
|
2998
|
+
*
|
|
2999
|
+
* For some language features, ty can also understand conditionals based on comparisons
|
|
3000
|
+
* with `sys.version_info`. These are commonly found in typeshed, for example,
|
|
3001
|
+
* to reflect the differing contents of the standard library across Python versions.
|
|
3002
|
+
*/
|
|
3003
|
+
'python-version'?: PythonVersion | null;
|
|
3004
|
+
/**
|
|
3005
|
+
* The root paths of the project, used for finding first-party modules.
|
|
3006
|
+
*
|
|
3007
|
+
* Accepts a list of directory paths searched in priority order (first has highest priority).
|
|
3008
|
+
*
|
|
3009
|
+
* If left unspecified, ty will try to detect common project layouts and initialize `root` accordingly.
|
|
3010
|
+
* The project root (`.`) is always included. Additionally, the following directories are included
|
|
3011
|
+
* if they exist and are not packages (i.e. they do not contain `__init__.py` or `__init__.pyi` files):
|
|
3012
|
+
*
|
|
3013
|
+
* * `./src`
|
|
3014
|
+
* * `./<project-name>` (if a `./<project-name>/<project-name>` directory exists)
|
|
3015
|
+
* * `./python`
|
|
3016
|
+
*/
|
|
3017
|
+
root?: RelativePathBuf[] | null;
|
|
3018
|
+
/**
|
|
3019
|
+
* Optional path to a "typeshed" directory on disk for us to use for standard-library types.
|
|
3020
|
+
* If this is not provided, we will fallback to our vendored typeshed stubs for the stdlib,
|
|
3021
|
+
* bundled as a zip file in the binary
|
|
3022
|
+
*/
|
|
3023
|
+
typeshed?: RelativePathBuf | null;
|
|
3024
|
+
}
|
|
3025
|
+
export interface OverrideOptions {
|
|
3026
|
+
analysis?: AnalysisOptions | null;
|
|
3027
|
+
/**
|
|
3028
|
+
* A list of file and directory patterns to exclude from this override.
|
|
3029
|
+
*
|
|
3030
|
+
* Patterns follow a syntax similar to `.gitignore`.
|
|
3031
|
+
* Exclude patterns take precedence over include patterns within the same override.
|
|
3032
|
+
*
|
|
3033
|
+
* If not specified, defaults to `[]` (excludes no files).
|
|
3034
|
+
*/
|
|
3035
|
+
exclude?: ArrayOfString | null;
|
|
3036
|
+
/**
|
|
3037
|
+
* A list of file and directory patterns to include for this override.
|
|
3038
|
+
*
|
|
3039
|
+
* The `include` option follows a similar syntax to `.gitignore` but reversed:
|
|
3040
|
+
* Including a file or directory will make it so that it (and its contents)
|
|
3041
|
+
* are affected by this override.
|
|
3042
|
+
*
|
|
3043
|
+
* If not specified, defaults to `["**"]` (matches all files).
|
|
3044
|
+
*/
|
|
3045
|
+
include?: ArrayOfString | null;
|
|
3046
|
+
/**
|
|
3047
|
+
* Rule overrides for files matching the include/exclude patterns.
|
|
3048
|
+
*
|
|
3049
|
+
* These rules will be merged with the global rules, with override rules
|
|
3050
|
+
* taking precedence for matching files. You can set rules to different
|
|
3051
|
+
* severity levels or disable them entirely.
|
|
3052
|
+
*/
|
|
3053
|
+
rules?: Rules | null;
|
|
3054
|
+
}
|
|
3055
|
+
export interface Rules {
|
|
3056
|
+
'abstract-method-in-final-class'?: DetectsFinalClassesWithUnimplementedAbstractMethods;
|
|
3057
|
+
all?: SetTheDefaultSeverityLevelForAllRules;
|
|
3058
|
+
'ambiguous-protocol-member'?: DetectsProtocolClassesWithAmbiguousInterfaces;
|
|
3059
|
+
'assert-type-unspellable-subtype'?: DetectsFailedTypeAssertions;
|
|
3060
|
+
'byte-string-type-annotation'?: DetectsByteStringsInTypeAnnotationPositions;
|
|
3061
|
+
'call-abstract-method'?: DetectsCallsToAbstractMethodsWithTrivialBodiesOnClassObjects;
|
|
3062
|
+
'call-non-callable'?: DetectsCallsToNonCallableObjects;
|
|
3063
|
+
'call-top-callable'?: DetectsCallsToTheTopCallableType;
|
|
3064
|
+
'conflicting-argument-forms'?: DetectsWhenAnArgumentIsUsedAsBothAValueAndATypeFormInACall;
|
|
3065
|
+
'conflicting-declarations'?: DetectsConflictingDeclarations;
|
|
3066
|
+
'conflicting-metaclass'?: DetectsConflictingMetaclasses;
|
|
3067
|
+
'cyclic-class-definition'?: DetectsCyclicClassDefinitions;
|
|
3068
|
+
'cyclic-type-alias-definition'?: DetectsCyclicTypeAliasDefinitions;
|
|
3069
|
+
'dataclass-field-order'?: DetectsDataclassDefinitionsWithRequiredFieldsAfterFieldsWithDefaultValues;
|
|
3070
|
+
deprecated?: DetectsUsesOfDeprecatedItems;
|
|
3071
|
+
'division-by-zero'?: DetectsDivisionByZero;
|
|
3072
|
+
'duplicate-base'?: DetectsClassDefinitionsWithDuplicateBases;
|
|
3073
|
+
'duplicate-kw-only'?: DetectsDataclassDefinitionsWithMoreThanOneUsageOfKW_ONLY;
|
|
3074
|
+
'empty-body'?: DetectsFunctionsWithEmptyBodiesThatHaveANonNoneReturnTypeAnnotation;
|
|
3075
|
+
'escape-character-in-forward-annotation'?: DetectsForwardTypeAnnotationsWithEscapeCharacters;
|
|
3076
|
+
'final-on-non-method'?: DetectsFinalAppliedToNonMethodFunctions;
|
|
3077
|
+
'final-without-value'?: DetectsFinalDeclarationsWithoutAValue;
|
|
3078
|
+
'fstring-type-annotation'?: DetectsFStringsInTypeAnnotationPositions;
|
|
3079
|
+
'ignore-comment-unknown-rule'?: DetectsTyIgnoreCommentsThatReferenceUnknownRules;
|
|
3080
|
+
'implicit-concatenated-string-type-annotation'?: DetectsImplicitConcatenatedStringsInTypeAnnotations;
|
|
3081
|
+
'inconsistent-mro'?: DetectsClassDefinitionsWithAnInconsistentMRO;
|
|
3082
|
+
'index-out-of-bounds'?: DetectsIndexOutOfBoundsErrors;
|
|
3083
|
+
'ineffective-final'?: DetectsCallsToFinalThatTypeCheckersCannotInterpret;
|
|
3084
|
+
'instance-layout-conflict'?: DetectsClassDefinitionsThatRaiseTypeErrorDueToInstanceLayoutConflict;
|
|
3085
|
+
'invalid-argument-type'?: DetectsCallArgumentsWhoseTypeIsNotAssignableToTheCorrespondingTypedParameter;
|
|
3086
|
+
'invalid-assignment'?: DetectsInvalidAssignments;
|
|
3087
|
+
'invalid-attribute-access'?: InvalidAttributeAccess;
|
|
3088
|
+
'invalid-await'?: DetectsAwaitingOnTypesThatDonTSupportIt;
|
|
3089
|
+
'invalid-base'?: DetectsClassBasesThatWillCauseTheClassDefinitionToRaiseAnExceptionAtRuntime;
|
|
3090
|
+
'invalid-context-manager'?: DetectsExpressionsUsedInWithStatementsThatDonTImplementTheContextManagerProtocol;
|
|
3091
|
+
'invalid-dataclass'?: DetectsInvalidDataclassApplications;
|
|
3092
|
+
'invalid-dataclass-override'?: DetectsDataclassesWithFrozenTrueThatHaveACustom_Setattr__Or_Delattr__Implementation;
|
|
3093
|
+
'invalid-declaration'?: DetectsInvalidDeclarations;
|
|
3094
|
+
'invalid-enum-member-annotation'?: DetectsTypeAnnotationsOnEnumMembers;
|
|
3095
|
+
'invalid-exception-caught'?: DetectsExceptionHandlersThatCatchClassesThatDoNotInheritFromBaseException;
|
|
3096
|
+
'invalid-explicit-override'?: DetectsMethodsThatAreDecoratedWithOverrideButDoNotOverrideAnyMethodInASuperclass;
|
|
3097
|
+
'invalid-frozen-dataclass-subclass'?: DetectsDataclassesWithInvalidFrozenNonFrozenSubclassing;
|
|
3098
|
+
'invalid-generic-class'?: DetectsInvalidGenericClasses;
|
|
3099
|
+
'invalid-generic-enum'?: DetectsGenericEnumClasses;
|
|
3100
|
+
'invalid-ignore-comment'?: DetectsIgnoreCommentsThatUseInvalidSyntax;
|
|
3101
|
+
'invalid-key'?: DetectsInvalidSubscriptAccessesOrTypedDictLiteralKeys;
|
|
3102
|
+
'invalid-legacy-positional-parameter'?: DetectsIncorrectUsageOfTheLegacyConventionForSpecifyingPositionalOnlyParameters;
|
|
3103
|
+
'invalid-legacy-type-variable'?: DetectsInvalidLegacyTypeVariables;
|
|
3104
|
+
'invalid-match-pattern'?: DetectInvalidMatchPatterns;
|
|
3105
|
+
'invalid-metaclass'?: DetectsInvalidMetaclassArguments;
|
|
3106
|
+
'invalid-method-override'?: DetectsMethodDefinitionsThatViolateTheLiskovSubstitutionPrinciple;
|
|
3107
|
+
'invalid-named-tuple'?: DetectsInvalidNamedTupleClassDefinitions;
|
|
3108
|
+
'invalid-newtype'?: DetectsInvalidNewTypeDefinitions;
|
|
3109
|
+
'invalid-overload'?: DetectsInvalidOverloadUsages;
|
|
3110
|
+
'invalid-parameter-default'?: DetectsDefaultValuesThatCanTBeAssignedToTheParameterSAnnotatedType;
|
|
3111
|
+
'invalid-paramspec'?: DetectsInvalidParamSpecUsage;
|
|
3112
|
+
'invalid-protocol'?: DetectsInvalidProtocolClassDefinitions;
|
|
3113
|
+
'invalid-raise'?: DetectsRaiseStatementsThatRaiseInvalidExceptionsOrUseInvalidCauses;
|
|
3114
|
+
'invalid-return-type'?: DetectsReturnedValuesThatCanTBeAssignedToTheFunctionSAnnotatedReturnType;
|
|
3115
|
+
'invalid-super-argument'?: DetectsInvalidArgumentsForSuper;
|
|
3116
|
+
'invalid-syntax-in-forward-annotation'?: DetectsInvalidSyntaxInForwardAnnotations;
|
|
3117
|
+
'invalid-total-ordering'?: DetectsTotalOrderingClassesWithoutAnOrderingMethod;
|
|
3118
|
+
'invalid-type-alias-type'?: DetectsInvalidTypeAliasTypeDefinitions;
|
|
3119
|
+
'invalid-type-arguments'?: DetectsInvalidTypeArgumentsInGenericSpecialization;
|
|
3120
|
+
'invalid-type-checking-constant'?: DetectsInvalidTYPE_CHECKINGConstantAssignments;
|
|
3121
|
+
'invalid-type-form'?: DetectsInvalidTypeForms;
|
|
3122
|
+
'invalid-type-guard-call'?: DetectsTypeGuardFunctionCallsThatHasNoNarrowingEffect;
|
|
3123
|
+
'invalid-type-guard-definition'?: DetectsMalformedTypeGuardFunctions;
|
|
3124
|
+
'invalid-type-variable-bound'?: DetectsInvalidTypeVariableBounds;
|
|
3125
|
+
'invalid-type-variable-constraints'?: DetectsInvalidTypeVariableConstraints;
|
|
3126
|
+
'invalid-type-variable-default'?: DetectsInvalidTypeVariableDefaults;
|
|
3127
|
+
'invalid-typed-dict-header'?: DetectsInvalidStatementsInTypedDictClassHeaders;
|
|
3128
|
+
'invalid-typed-dict-statement'?: DetectsInvalidStatementsInTypedDictClassBodies;
|
|
3129
|
+
'isinstance-against-protocol'?: ReportsInvalidRuntimeChecksAgainstProtocolClasses;
|
|
3130
|
+
'isinstance-against-typed-dict'?: ReportsRuntimeChecksAgainstTypedDictClasses;
|
|
3131
|
+
'missing-argument'?: DetectsMissingRequiredArgumentsInACall;
|
|
3132
|
+
'missing-typed-dict-key'?: DetectsMissingRequiredKeysInTypedDictConstructors;
|
|
3133
|
+
'no-matching-overload'?: DetectsCallsThatDoNotMatchAnyOverload;
|
|
3134
|
+
'not-iterable'?: DetectsIterationOverAnObjectThatIsNotIterable;
|
|
3135
|
+
'not-subscriptable'?: DetectsSubscriptingObjectsThatDoNotSupportSubscripting;
|
|
3136
|
+
'override-of-final-method'?: DetectsOverridesOfFinalMethods;
|
|
3137
|
+
'override-of-final-variable'?: DetectsOverridesOfFinalClassVariables;
|
|
3138
|
+
'parameter-already-assigned'?: DetectsMultipleArgumentsForTheSameParameter;
|
|
3139
|
+
'positional-only-parameter-as-kwarg'?: DetectsPositionalOnlyParametersPassedAsKeywordArguments;
|
|
3140
|
+
'possibly-missing-attribute'?: DetectsReferencesToPossiblyMissingAttributes;
|
|
3141
|
+
'possibly-missing-implicit-call'?: DetectsImplicitCallsToPossiblyMissingMethods;
|
|
3142
|
+
'possibly-missing-import'?: DetectsPossiblyMissingImports;
|
|
3143
|
+
'possibly-missing-submodule'?: DetectsAccessesOfSubmodulesThatMayNotBeAvailableAsAttributesOnTheirParentModule;
|
|
3144
|
+
'possibly-unresolved-reference'?: DetectsReferencesToPossiblyUndefinedNames;
|
|
3145
|
+
'raw-string-type-annotation'?: DetectsRawStringsInTypeAnnotationPositions;
|
|
3146
|
+
'redundant-cast'?: DetectsRedundantCastCalls;
|
|
3147
|
+
'redundant-final-classvar'?: DetectsRedundantCombinationsOfClassVarAndFinal;
|
|
3148
|
+
'shadowed-type-variable'?: DetectsTypeVariablesThatShadowTypeVariablesFromOuterScopes;
|
|
3149
|
+
'static-assert-error'?: FailedStaticAssertion;
|
|
3150
|
+
'subclass-of-final-class'?: DetectsSubclassesOfFinalClasses;
|
|
3151
|
+
'super-call-in-named-tuple-method'?: DetectsSuperCallsInMethodsOfNamedTupleClasses;
|
|
3152
|
+
'too-many-positional-arguments'?: DetectsCallsPassingTooManyPositionalArguments;
|
|
3153
|
+
'type-assertion-failure'?: DetectsFailedTypeAssertions1;
|
|
3154
|
+
'unavailable-implicit-super-arguments'?: DetectsInvalidSuperCallsWhereImplicitArgumentsAreUnavailable;
|
|
3155
|
+
'unbound-type-variable'?: DetectsTypeVariablesUsedOutsideOfTheirBoundScope;
|
|
3156
|
+
'undefined-reveal'?: DetectsUsagesOfRevealTypeWithoutImportingIt;
|
|
3157
|
+
'unknown-argument'?: DetectsUnknownKeywordArgumentsInCalls;
|
|
3158
|
+
'unresolved-attribute'?: DetectsReferencesToUnresolvedAttributes;
|
|
3159
|
+
'unresolved-global'?: DetectsGlobalStatementsWithNoDefinitionInTheGlobalScope;
|
|
3160
|
+
'unresolved-import'?: DetectsUnresolvedImports;
|
|
3161
|
+
'unresolved-reference'?: DetectsReferencesToNamesThatAreNotDefined;
|
|
3162
|
+
'unsupported-base'?: DetectsClassBasesThatAreUnsupportedAsTyCouldNotFeasiblyCalculateTheClassSMRO;
|
|
3163
|
+
'unsupported-bool-conversion'?: DetectsBooleanConversionWhereTheObjectIncorrectlyImplements_Bool__;
|
|
3164
|
+
'unsupported-dynamic-base'?: DetectsDynamicClassBasesThatAreUnsupportedAsTyCouldNotFeasiblyCalculateTheClassSMRO;
|
|
3165
|
+
'unsupported-operator'?: DetectsBinaryUnaryOrComparisonExpressionsWhereTheOperandsDonTSupportTheOperator;
|
|
3166
|
+
'unused-awaitable'?: DetectsAwaitableObjectsThatAreUsedAsExpressionStatementsWithoutBeingAwaited;
|
|
3167
|
+
'unused-ignore-comment'?: DetectsUnusedTyIgnoreComments;
|
|
3168
|
+
'unused-type-ignore-comment'?: DetectsUnusedTypeIgnoreComments;
|
|
3169
|
+
'useless-overload-body'?: DetectsOverloadDecoratedFunctionsWithNonStubBodies;
|
|
3170
|
+
'zero-stepsize-in-slice'?: DetectsASliceStepSizeOfZero;
|
|
3171
|
+
[k: string]: Level;
|
|
3172
|
+
}
|
|
3173
|
+
export interface SrcOptions {
|
|
3174
|
+
/**
|
|
3175
|
+
* A list of file and directory patterns to exclude from type checking.
|
|
3176
|
+
*
|
|
3177
|
+
* Patterns follow a syntax similar to `.gitignore`:
|
|
3178
|
+
*
|
|
3179
|
+
* - `./src/` matches only a directory
|
|
3180
|
+
* - `./src` matches both files and directories
|
|
3181
|
+
* - `src` matches files or directories named `src`
|
|
3182
|
+
* - `*` matches any (possibly empty) sequence of characters (except `/`).
|
|
3183
|
+
* - `**` matches zero or more path components.
|
|
3184
|
+
* This sequence **must** form a single path component, so both `**a` and `b**` are invalid and will result in an error.
|
|
3185
|
+
* A sequence of more than two consecutive `*` characters is also invalid.
|
|
3186
|
+
* - `?` matches any single character except `/`
|
|
3187
|
+
* - `[abc]` matches any character inside the brackets. Character sequences can also specify ranges of characters, as ordered by Unicode,
|
|
3188
|
+
* so e.g. `[0-9]` specifies any character between `0` and `9` inclusive. An unclosed bracket is invalid.
|
|
3189
|
+
* - `!pattern` negates a pattern (undoes the exclusion of files that would otherwise be excluded)
|
|
3190
|
+
*
|
|
3191
|
+
* All paths are anchored relative to the project root (`src` only
|
|
3192
|
+
* matches `<project_root>/src` and not `<project_root>/test/src`).
|
|
3193
|
+
* To exclude any directory or file named `src`, use `** /src` instead.
|
|
3194
|
+
*
|
|
3195
|
+
* By default, ty excludes commonly ignored directories:
|
|
3196
|
+
*
|
|
3197
|
+
* - `** /.bzr/`
|
|
3198
|
+
* - `** /.direnv/`
|
|
3199
|
+
* - `** /.eggs/`
|
|
3200
|
+
* - `** /.git/`
|
|
3201
|
+
* - `** /.git-rewrite/`
|
|
3202
|
+
* - `** /.hg/`
|
|
3203
|
+
* - `** /.mypy_cache/`
|
|
3204
|
+
* - `** /.nox/`
|
|
3205
|
+
* - `** /.pants.d/`
|
|
3206
|
+
* - `** /.pytype/`
|
|
3207
|
+
* - `** /.ruff_cache/`
|
|
3208
|
+
* - `** /.svn/`
|
|
3209
|
+
* - `** /.tox/`
|
|
3210
|
+
* - `** /.venv/`
|
|
3211
|
+
* - `** /__pypackages__/`
|
|
3212
|
+
* - `** /_build/`
|
|
3213
|
+
* - `** /buck-out/`
|
|
3214
|
+
* - `** /dist/`
|
|
3215
|
+
* - `** /node_modules/`
|
|
3216
|
+
* - `** /venv/`
|
|
3217
|
+
*
|
|
3218
|
+
* You can override any default exclude by using a negated pattern. For example,
|
|
3219
|
+
* to re-include `dist` use `exclude = ["!dist"]`
|
|
3220
|
+
*/
|
|
3221
|
+
exclude?: ArrayOfString | null;
|
|
3222
|
+
/**
|
|
3223
|
+
* A list of files and directories to check. The `include` option
|
|
3224
|
+
* follows a similar syntax to `.gitignore` but reversed:
|
|
3225
|
+
* Including a file or directory will make it so that it (and its contents)
|
|
3226
|
+
* are type checked.
|
|
3227
|
+
*
|
|
3228
|
+
* - `./src/` matches only a directory
|
|
3229
|
+
* - `./src` matches both files and directories
|
|
3230
|
+
* - `src` matches a file or directory named `src`
|
|
3231
|
+
* - `*` matches any (possibly empty) sequence of characters (except `/`).
|
|
3232
|
+
* - `**` matches zero or more path components.
|
|
3233
|
+
* This sequence **must** form a single path component, so both `**a` and `b**` are invalid and will result in an error.
|
|
3234
|
+
* A sequence of more than two consecutive `*` characters is also invalid.
|
|
3235
|
+
* - `?` matches any single character except `/`
|
|
3236
|
+
* - `[abc]` matches any character inside the brackets. Character sequences can also specify ranges of characters, as ordered by Unicode,
|
|
3237
|
+
* so e.g. `[0-9]` specifies any character between `0` and `9` inclusive. An unclosed bracket is invalid.
|
|
3238
|
+
*
|
|
3239
|
+
* All paths are anchored relative to the project root (`src` only
|
|
3240
|
+
* matches `<project_root>/src` and not `<project_root>/test/src`).
|
|
3241
|
+
*
|
|
3242
|
+
* `exclude` takes precedence over `include`.
|
|
3243
|
+
*/
|
|
3244
|
+
include?: ArrayOfString | null;
|
|
3245
|
+
/**
|
|
3246
|
+
* Whether to automatically exclude files that are ignored by `.ignore`,
|
|
3247
|
+
* `.gitignore`, `.git/info/exclude`, and global `gitignore` files.
|
|
3248
|
+
* Enabled by default.
|
|
3249
|
+
*/
|
|
3250
|
+
'respect-ignore-files'?: boolean | null;
|
|
3251
|
+
/**
|
|
3252
|
+
* @deprecated
|
|
3253
|
+
* The root of the project, used for finding first-party modules.
|
|
3254
|
+
*
|
|
3255
|
+
* If left unspecified, ty will try to detect common project layouts and initialize `src.root` accordingly.
|
|
3256
|
+
* The project root (`.`) is always included. Additionally, the following directories are included
|
|
3257
|
+
* if they exist and are not packages (i.e. they do not contain `__init__.py` or `__init__.pyi` files):
|
|
3258
|
+
*
|
|
3259
|
+
* * `./src`
|
|
3260
|
+
* * `./<project-name>` (if a `./<project-name>/<project-name>` directory exists)
|
|
3261
|
+
* * `./python`
|
|
3262
|
+
*/
|
|
3263
|
+
root?: RelativePathBuf | null;
|
|
3264
|
+
}
|
|
3265
|
+
export interface TerminalOptions {
|
|
3266
|
+
/**
|
|
3267
|
+
* Use exit code 1 if there are any warning-level diagnostics.
|
|
3268
|
+
*
|
|
3269
|
+
* Defaults to `false`.
|
|
3270
|
+
*/
|
|
3271
|
+
'error-on-warning'?: boolean | null;
|
|
3272
|
+
/**
|
|
3273
|
+
* The format to use for printing diagnostic messages.
|
|
3274
|
+
*
|
|
3275
|
+
* Defaults to `full`.
|
|
3276
|
+
*/
|
|
3277
|
+
'output-format'?: OutputFormat | null;
|
|
3278
|
+
}
|