Support local and dynamic class- and static-method decorators (#8592)

## Summary

This brings ruff's behavior in line with what `pep8-naming` already does
and thus closes #8397.

I had initially implemented this to look at the last segment of a dotted
path only when the entry in the `*-decorators` setting started with a
`.`, but in the end I thought it's better to remain consistent w/
`pep8-naming` and doing a match against the last segment of the
decorator name in any case.

If you prefer to diverge from this in favor of less ambiguity in the
configuration let me know and I'll change it so you would need to put
e.g. `.expression` in the `classmethod-decorators` list.

## Test Plan

Tested against the file in the issue linked below, plus the new testcase
added in this PR.
This commit is contained in:
Adrian 2023-11-10 03:04:25 +01:00 committed by GitHub
parent 565ddebb15
commit 4ebd0bd31e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 279 additions and 33 deletions

View file

@ -2242,13 +2242,20 @@ pub struct Pep8NamingOptions {
/// in this list takes a `cls` argument as its first argument.
///
/// Expects to receive a list of fully-qualified names (e.g., `pydantic.validator`,
/// rather than `validator`).
/// rather than `validator`) or alternatively a plain name which is then matched against
/// the last segment in case the decorator itself consists of a dotted name.
#[option(
default = r#"[]"#,
value_type = "list[str]",
example = r#"
# Allow Pydantic's `@validator` decorator to trigger class method treatment.
classmethod-decorators = ["pydantic.validator"]
classmethod-decorators = [
# Allow Pydantic's `@validator` decorator to trigger class method treatment.
"pydantic.validator",
# Allow SQLAlchemy's dynamic decorators, like `@field.expression`, to trigger class method treatment.
"declared_attr",
"expression",
"comparator",
]
"#
)]
pub classmethod_decorators: Option<Vec<String>>,
@ -2261,7 +2268,8 @@ pub struct Pep8NamingOptions {
/// in this list has no `self` or `cls` argument.
///
/// Expects to receive a list of fully-qualified names (e.g., `belay.Device.teardown`,
/// rather than `teardown`).
/// rather than `teardown`) or alternatively a plain name which is then matched against
/// the last segment in case the decorator itself consists of a dotted name.
#[option(
default = r#"[]"#,
value_type = "list[str]",