Improve dynamically-typed-expression documentation

This commit is contained in:
Charlie Marsh 2023-02-10 17:55:26 -05:00
parent 3d8fb5be20
commit 6376e5915e
2 changed files with 27 additions and 9 deletions

View file

@ -374,12 +374,16 @@ impl Violation for MissingReturnTypeClassMethod {
define_violation!(
/// ### What it does
/// Checks that an expression is annotated with a more specific type than `Any`.
/// Checks that an expression is annotated with a more specific type than
/// `Any`.
///
/// ### Why is this bad?
/// `Any` is a type that can be anything, and it is the default type for
/// unannotated expressions. It is better to be explicit about the type of an
/// expression, and to use `Any` only when it is really needed.
/// `Any` is a special type indicating an unconstrained type. When an
/// expression is annotated with type `Any`, type checkers will allow all
/// operations on it.
///
/// It's better to be explicit about the type of an expression, and to use
/// `Any` as an "escape hatch" only when it is really needed.
///
/// ### Example
/// ```python
@ -392,6 +396,11 @@ define_violation!(
/// def foo(x: int):
/// ...
/// ```
///
/// ### References
/// * [PEP 484](https://www.python.org/dev/peps/pep-0484/#the-any-type)
/// * [`typing.Any`](https://docs.python.org/3/library/typing.html#typing.Any)
/// * [Mypy: The Any type](https://mypy.readthedocs.io/en/stable/kinds_of_types.html#the-any-type)
pub struct DynamicallyTypedExpression {
pub name: String,
}

View file

@ -3,12 +3,16 @@
Derived from the **flake8-annotations** linter.
### What it does
Checks that an expression is annotated with a more specific type than `Any`.
Checks that an expression is annotated with a more specific type than
`Any`.
### Why is this bad?
`Any` is a type that can be anything, and it is the default type for
unannotated expressions. It is better to be explicit about the type of an
expression, and to use `Any` only when it is really needed.
`Any` is a special type indicating an unconstrained type. When an
expression is annotated with type `Any`, type checkers will allow all
operations on it.
It's better to be explicit about the type of an expression, and to use
`Any` as an "escape hatch" only when it is really needed.
### Example
```python
@ -20,4 +24,9 @@ Use instead:
```python
def foo(x: int):
...
```
```
### References
* [PEP 484](https://www.python.org/dev/peps/pep-0484/#the-any-type)
* [`typing.Any`](https://docs.python.org/3/library/typing.html#typing.Any)
* [Mypy: The Any type](https://mypy.readthedocs.io/en/stable/kinds_of_types.html#the-any-type)