Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Merged
merged 1 commit into from
Sep 10, 2024

Conversation

augustelalande
Copy link
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.

Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
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 merged commit d6bd841 into astral-sh:main Sep 10, 2024
20 checks passed
@AlexWaygood
Copy link
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 :-)

# for free to join this conversation on GitHub. Already have an account? # 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