diff --git a/newsfragments/6607.changed.md b/newsfragments/6607.changed.md new file mode 100644 index 00000000000..fc3805b6ead --- /dev/null +++ b/newsfragments/6607.changed.md @@ -0,0 +1 @@ +srub: do not emit `__str__` and `__repr__` methods with the same signature as their `object` counterpart in the generated stubs diff --git a/pyo3-introspection/src/stubs.rs b/pyo3-introspection/src/stubs.rs index 56c0cdd7d30..04d861634b0 100644 --- a/pyo3-introspection/src/stubs.rs +++ b/pyo3-introspection/src/stubs.rs @@ -189,6 +189,7 @@ fn is_type_check_only(class: &Class) -> bool { } fn class_stubs(class: &Class, imports: &Imports) -> String { + let methods = emitted_methods(class).collect::>(); let mut buffer = String::new(); for decorator in &class.decorators { buffer.push('@'); @@ -209,7 +210,7 @@ fn class_stubs(class: &Class, imports: &Imports) -> String { } buffer.push(':'); if class.docstring.is_none() - && class.methods.is_empty() + && methods.is_empty() && class.attributes.is_empty() && class.inner_classes.is_empty() { @@ -223,7 +224,7 @@ fn class_stubs(class: &Class, imports: &Imports) -> String { buffer.push_str("\n "); push_indented(&mut buffer, " ", &attribute_stubs(attribute, imports)); } - for method in &class.methods { + for method in methods { // We do the indentation buffer.push_str("\n "); push_indented( @@ -240,6 +241,49 @@ fn class_stubs(class: &Class, imports: &Imports) -> String { buffer } +/// The methods to write in the class stub +fn emitted_methods(class: &Class) -> impl Iterator { + class + .methods + .iter() + .filter(|method| !is_redundant_object_method(method)) +} + +/// `__str__` and `__repr__` with the exact same signature as their `object` counterpart are +/// inherited from `object` by every class, so declaring them again is redundant (ruff `PYI029`). +/// +/// Methods with a docstring are kept to not lose documentation. +fn is_redundant_object_method(method: &Function) -> bool { + let Arguments { + positional_only_arguments, + arguments, + vararg, + keyword_only_arguments, + kwarg, + } = &method.arguments; + matches!(method.name.as_str(), "__str__" | "__repr__") + && method.decorators.is_empty() + && !method.is_async + && method.docstring.is_none() + && matches!( + positional_only_arguments.as_slice(), + [Argument { + annotation: None, + default_value: None, + .. + }] + ) + && arguments.is_empty() + && vararg.is_none() + && keyword_only_arguments.is_empty() + && kwarg.is_none() + && matches!( + &method.returns, + Some(Expr::Attribute { value, attr }) + if attr == "str" && matches!(&**value, Expr::Name { id } if id == "builtins") + ) +} + fn function_stubs(function: &Function, imports: &Imports, class_name: Option<&str>) -> String { // Signature let mut parameters = Vec::new(); @@ -687,7 +731,7 @@ impl ReferencedNames { for decorator in &class.decorators { self.walk_expr(decorator); } - for method in &class.methods { + for method in emitted_methods(class) { self.walk_function(method); } for attr in &class.attributes { diff --git a/pytests/pyproject.toml b/pytests/pyproject.toml index ee3eddc2372..1fd0ac2790b 100644 --- a/pytests/pyproject.toml +++ b/pytests/pyproject.toml @@ -27,7 +27,6 @@ requires-python = ">=3.9" "stubs/*.pyi" = [ "I001", # unsorted imports "PYI020", # quoted annotations - "PYI029", # redundant `__str__` definition "UP037", # quoted annotations ] diff --git a/pytests/stubs/comparisons.pyi b/pytests/stubs/comparisons.pyi index eb093ea508c..1c44dc4662c 100644 --- a/pytests/stubs/comparisons.pyi +++ b/pytests/stubs/comparisons.pyi @@ -56,7 +56,6 @@ class OrderedDerived: def __lt__(self, value: object, /) -> bool: ... def __ne__(self, value: object, /) -> bool: ... def __new__(cls, /, value: SupportsIndex) -> Self: ... - def __str__(self, /) -> str: ... @final class OrderedRichCmp: diff --git a/pytests/stubs/enums.pyi b/pytests/stubs/enums.pyi index b70f47368c0..84b5db380f8 100644 --- a/pytests/stubs/enums.pyi +++ b/pytests/stubs/enums.pyi @@ -100,14 +100,12 @@ class SimpleEnum: def __eq__(self, value: object, /) -> bool: ... def __int__(self, /) -> int: ... def __ne__(self, value: object, /) -> bool: ... - def __repr__(self, /) -> str: ... @final class SimpleEnumWithoutDerive: A: Final[SimpleEnumWithoutDerive] B: Final[SimpleEnumWithoutDerive] def __int__(self, /) -> int: ... - def __repr__(self, /) -> str: ... @disjoint_base class SimpleTupleEnum: diff --git a/pytests/stubs/pyclasses.pyi b/pytests/stubs/pyclasses.pyi index d65b313246c..aa23baee885 100644 --- a/pytests/stubs/pyclasses.pyi +++ b/pytests/stubs/pyclasses.pyi @@ -100,9 +100,7 @@ class Number: def __or__(self, other: object, /) -> Number: ... def __pos__(self, /) -> Number: ... def __pow__(self, other: object, modulo: object = None, /) -> Number: ... - def __repr__(self, /) -> str: ... def __rshift__(self, other: object, /) -> Number: ... - def __str__(self, /) -> str: ... def __sub__(self, other: object, /) -> Number: ... def __truediv__(self, other: object, /) -> Number: ... def __xor__(self, other: object, /) -> Number: ... diff --git a/pytests/stubs/subclassing.pyi b/pytests/stubs/subclassing.pyi index 95a06f60601..c6ac2649670 100644 --- a/pytests/stubs/subclassing.pyi +++ b/pytests/stubs/subclassing.pyi @@ -6,14 +6,11 @@ __all__ = ["SubDict", "Subclass", "Subclassable"] @final class SubDict(dict): def __new__(cls, /) -> Self: ... - def __str__(self, /) -> str: ... @final class Subclass(Subclassable): def __new__(cls, /) -> Self: ... - def __str__(self, /) -> str: ... @disjoint_base class Subclassable: def __new__(cls, /) -> Self: ... - def __str__(self, /) -> str: ...