Avoid reformatting comments in rules reference documentation (#19093)

closes https://github.com/astral-sh/ty/issues/754
This commit is contained in:
Zanie Blue 2025-07-02 10:16:44 -05:00 committed by GitHub
parent 4cf56d7ad4
commit efd9b75352
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 28 deletions

View file

@ -73,11 +73,16 @@ fn generate_markdown() -> String {
for lint in lints { for lint in lints {
let _ = writeln!(&mut output, "## `{rule_name}`\n", rule_name = lint.name()); let _ = writeln!(&mut output, "## `{rule_name}`\n", rule_name = lint.name());
// Increase the header-level by one // Reformat headers as bold text
let mut in_code_fence = false;
let documentation = lint let documentation = lint
.documentation_lines() .documentation_lines()
.map(|line| { .map(|line| {
if line.starts_with('#') { // Toggle the code fence state if we encounter a boundary
if line.starts_with("```") {
in_code_fence = !in_code_fence;
}
if !in_code_fence && line.starts_with('#') {
Cow::Owned(format!( Cow::Owned(format!(
"**{line}**\n", "**{line}**\n",
line = line.trim_start_matches('#').trim_start() line = line.trim_start_matches('#').trim_start()

View file

@ -138,8 +138,7 @@ class M2(type): ...
class A(metaclass=M1): ... class A(metaclass=M1): ...
class B(metaclass=M2): ... class B(metaclass=M2): ...
**TypeError: metaclass conflict** # TypeError: metaclass conflict
class C(A, B): ... class C(A, B): ...
``` ```
@ -166,8 +165,7 @@ inherits from itself.
**Examples** **Examples**
```python ```python
**foo.pyi** # foo.pyi
class A(B): ... class A(B): ...
class B(A): ... class B(A): ...
``` ```
@ -195,8 +193,7 @@ Class definitions with duplicate bases raise `TypeError` at runtime.
```python ```python
class A: ... class A: ...
**TypeError: duplicate base class** # TypeError: duplicate base class
class B(A, A): ... class B(A, A): ...
``` ```
@ -326,8 +323,7 @@ Classes with an inconsistent MRO will raise a `TypeError` at runtime.
class A: ... class A: ...
class B(A): ... class B(A): ...
**TypeError: Cannot create a consistent method resolution order** # TypeError: Cannot create a consistent method resolution order
class C(A, B): ... class C(A, B): ...
``` ```
@ -397,8 +393,7 @@ class A:
class B: class B:
__slots__ = ("a", "b") # Even if the values are the same __slots__ = ("a", "b") # Even if the values are the same
**TypeError: multiple bases have instance lay-out conflict** # TypeError: multiple bases have instance lay-out conflict
class C(A, B): ... class C(A, B): ...
``` ```
@ -420,8 +415,7 @@ class B:
class C: class C:
__slots__ = ("a", "b") __slots__ = ("a", "b")
**fine** # fine
class D(A, B, C): ... class D(A, B, C): ...
``` ```
@ -571,8 +565,7 @@ Such a statement will raise `TypeError` at runtime.
**Examples** **Examples**
```python ```python
**TypeError: 'int' object does not support the context manager protocol** # TypeError: 'int' object does not support the context manager protocol
with 1: with 1:
print(2) print(2)
``` ```
@ -669,8 +662,7 @@ from typing import Generic, TypeVar
T = TypeVar("T") # okay T = TypeVar("T") # okay
**error: class uses both PEP-695 syntax and legacy syntax** # error: class uses both PEP-695 syntax and legacy syntax
class C[U](Generic[T]): ... class C[U](Generic[T]): ...
``` ```
@ -703,8 +695,7 @@ T = TypeVar("T") # okay
Q = TypeVar("S") # error: TypeVar name must match the variable it's assigned to Q = TypeVar("S") # error: TypeVar name must match the variable it's assigned to
T = TypeVar("T") # error: TypeVars should not be redefined T = TypeVar("T") # error: TypeVars should not be redefined
**error: TypeVar must be immediately assigned to a variable** # error: TypeVar must be immediately assigned to a variable
def f(t: TypeVar("U")): ... def f(t: TypeVar("U")): ...
``` ```
@ -736,8 +727,7 @@ as `type.__new__`.
```python ```python
def f(): ... def f(): ...
**TypeError: f() takes 0 positional arguments but 3 were given** # TypeError: f() takes 0 positional arguments but 3 were given
class B(metaclass=f): ... class B(metaclass=f): ...
``` ```
@ -1145,8 +1135,7 @@ T = TypeVar('T', str) # invalid constrained TypeVar
Use instead: Use instead:
```python ```python
T = TypeVar('T', str, int) # valid constrained TypeVar T = TypeVar('T', str, int) # valid constrained TypeVar
**or** # or
T = TypeVar('T', bound=str) # valid bound TypeVar T = TypeVar('T', bound=str) # valid bound TypeVar
``` ```
@ -1737,15 +1726,13 @@ or `ImportError` at runtime.
**Examples** **Examples**
```python ```python
**module.py** # module.py
import datetime import datetime
if datetime.date.today().weekday() != 6: if datetime.date.today().weekday() != 6:
a = 1 a = 1
**main.py** # main.py
from module import a # ImportError: cannot import name 'a' from 'module' from module import a # ImportError: cannot import name 'a' from 'module'
``` ```