mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:44:56 +00:00
[docs] flake8-self
Private member access docs (#2912)
This commit is contained in:
parent
cb971d3a48
commit
9168a12679
2 changed files with 37 additions and 1 deletions
|
@ -1287,7 +1287,7 @@ For more, see [flake8-self](https://pypi.org/project/flake8-self/) on PyPI.
|
||||||
|
|
||||||
| Code | Name | Message | Fix |
|
| Code | Name | Message | Fix |
|
||||||
| ---- | ---- | ------- | --- |
|
| ---- | ---- | ------- | --- |
|
||||||
| SLF001 | private-member-access | Private member accessed: `{access}` | |
|
| SLF001 | [private-member-access](https://beta.ruff.rs/docs/rules/private-member-access/) | Private member accessed: `{access}` | |
|
||||||
|
|
||||||
### flake8-simplify (SIM)
|
### flake8-simplify (SIM)
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,42 @@ use crate::registry::Diagnostic;
|
||||||
use crate::violation::Violation;
|
use crate::violation::Violation;
|
||||||
|
|
||||||
define_violation!(
|
define_violation!(
|
||||||
|
/// ## What it does
|
||||||
|
/// Checks for accesses on "private" class members.
|
||||||
|
///
|
||||||
|
/// ## Why is this bad?
|
||||||
|
/// In Python, the convention is such that class members that are prefixed
|
||||||
|
/// with a single underscore, or prefixed but not suffixed with a double
|
||||||
|
/// underscore, are considered private and intended for internal use.
|
||||||
|
///
|
||||||
|
/// Using such "private" members is considered a misuse of the class, as
|
||||||
|
/// there are no guarantees that the member will be present in future
|
||||||
|
/// versions, that it will have the same type, or that it will have the same
|
||||||
|
/// behavior. Instead, use the class's public interface.
|
||||||
|
///
|
||||||
|
/// ## Example
|
||||||
|
/// ```python
|
||||||
|
/// class Class:
|
||||||
|
/// def __init__(self):
|
||||||
|
/// self._private_member = "..."
|
||||||
|
///
|
||||||
|
/// var = Class()
|
||||||
|
/// print(var._private_member)
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// Use instead:
|
||||||
|
/// ```python
|
||||||
|
/// class Class:
|
||||||
|
/// def __init__(self):
|
||||||
|
/// self.public_member = "..."
|
||||||
|
///
|
||||||
|
/// var = Class()
|
||||||
|
/// print(var.public_member)
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// ## References
|
||||||
|
/// * [_What is the meaning of single or double underscores before an object name?_](https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-single-and-double-underscore-before-an-object-name)
|
||||||
|
/// ```
|
||||||
pub struct PrivateMemberAccess {
|
pub struct PrivateMemberAccess {
|
||||||
pub access: String,
|
pub access: String,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue