Skip to content

[pydoclint] Ignore DOC201 when function name is "__new__"#13300

Merged
AlexWaygood merged 1 commit intoastral-sh:mainfrom
augustelalande:doc201
Sep 10, 2024
Merged

[pydoclint] Ignore DOC201 when function name is "__new__"#13300
AlexWaygood merged 1 commit intoastral-sh:mainfrom
augustelalande:doc201

Conversation

@augustelalande
Copy link
Copy Markdown
Contributor

Summary

Resolves #13079.

Do not enforce the documentation of return in a __new__ function. Arguably this could apply to some other dunder methods.

Test Plan

Added fixture.

@github-actions
Copy link
Copy Markdown
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Copy Markdown
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is actually possible for __new__ methods to not return an instance of the class (e.g. https://github.com/python/cpython/blob/962304a54ca79da0838cf46dd4fb744045167cdd/Lib/pathlib/_local.py#L521-L524). But I agree that that's something of a rare edge case. We could in theory do some static analysis to figure out if __new__ returns an instance of the current class or not, but I think it's probably not worth it with our current type inference capabilities. So this LGTM.

I do also agree that we could probably extend it to some other dunders as well -- probably all of these?

/// Returns the expected return type for a magic method.
///
/// See: <https://github.com/JelleZijlstra/autotyping/blob/0adba5ba0eee33c1de4ad9d0c79acfd737321dd9/autotyping/autotyping.py#L69-L91>
pub fn simple_magic_return_type(method: &str) -> Option<&'static str> {
match method {
"__str__" | "__repr__" | "__format__" => Some("str"),
"__bytes__" => Some("bytes"),
"__len__" | "__length_hint__" | "__int__" | "__index__" => Some("int"),
"__float__" => Some("float"),
"__complex__" => Some("complex"),
"__bool__" | "__contains__" | "__instancecheck__" | "__subclasscheck__" => Some("bool"),
"__init__" | "__del__" | "__setattr__" | "__delattr__" | "__setitem__" | "__delitem__"
| "__set__" => Some("None"),
_ => None,
}
}

@MichaReiser MichaReiser added rule Implementing or modifying a lint rule preview Related to preview mode features labels Sep 10, 2024
@AlexWaygood AlexWaygood requested a review from carljm September 10, 2024 17:24
@AlexWaygood AlexWaygood merged commit d6bd841 into astral-sh:main Sep 10, 2024
@AlexWaygood
Copy link
Copy Markdown
Member

I do also agree that we could probably extend it to some other dunders as well -- probably all of these?

(These can be done as a followup, so I merged this for now :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview Related to preview mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docstring-missing-returns (DOC201) reports on __new__

3 participants