[flake8-self] Make SLF diagnostics robust to non-self-named variables#24281
[flake8-self] Make SLF diagnostics robust to non-self-named variables#24281charliermarsh merged 2 commits intomainfrom
flake8-self] Make SLF diagnostics robust to non-self-named variables#24281Conversation
|
flake8-self] Make SLF diagnostics robust to non-self-named variables
7eaaab9 to
0e83107
Compare
|
Hmm, while this is certainly more accurate, (almost) all ecosystem reports are now undesired false positives, where I'd prefer a few false negatives. |
|
I'm actually not sure that these are all false positives... The accesses within decorators seem like true positives? |
I didn't review all :) Which one is that? |
|
There are more false positives than I expected. The main pattern is mocking or patching existing classes. It might be better to suppress the diagnostic when |
|
@MichaReiser -- An example would be like the following from home-assistant: def async_log_errors[_DenonDeviceT: DenonDevice, **_P, _R](
func: Callable[Concatenate[_DenonDeviceT, _P], Awaitable[_R]],
) -> Callable[Concatenate[_DenonDeviceT, _P], Coroutine[Any, Any, _R | None]]:
"""Log errors occurred when calling a Denon AVR receiver.
Decorates methods of DenonDevice class.
Declaration of staticmethod for this method is at the end of this class.
"""
@wraps(func)
async def wrapper(
self: _DenonDeviceT, *args: _P.args, **kwargs: _P.kwargs
) -> _R | None:
available = True
try:
return await func(self, *args, **kwargs)
except AvrTimoutError:
available = False
if self.available:
_LOGGER.warning(
(
"Timeout connecting to Denon AVR receiver at host %s. "
"Device is unavailable"
),
self._receiver.host,
)
self._attr_available = False
except AvrNetworkError:
available = False
if self.available:
_LOGGER.warning(
(
"Network error connecting to Denon AVR receiver at host %s. "
"Device is unavailable"
),
self._receiver.host,
)
self._attr_available = False
except AvrProcessingError:
available = True
if self.available:
_LOGGER.warning(
(
"Update of Denon AVR receiver at host %s not complete. "
"Device is still available"
),
self._receiver.host,
)I would be fine to keep the blanket exemption on (at least) |
|
I guess let's ignore Ruff's semantic limitations for now. What's unclear to me is what the rule's desired behavior for method decorators? It seems a legitimate pattern, at least when the decorator is defined in the same file. Or are there reasons why method decorators should be forbidden to read private fields and, if so, what should users do instead? I'm okay with the blanked exemption of (Link to one of the homeassistant examples https://github.com/home-assistant/core/blob/31a24446a8a1aefa1f039b5dcf647dedcdf7f090/homeassistant/components/denonavr/media_player.py#L136) |
0e83107 to
acb05d4
Compare
Yeah, I think I agree here. I'm going to avoid removing the |
acb05d4 to
3986ef3
Compare
Summary
We allow private attribute access within the implementing class (e.g.,
self._foo), but historically, this was implemented by matching onself,cls, andmcs. So, e.g., if you used a self variable name other thanself, we'd still flag accesses.With this PR, we now model self correctly by detecting it as the "first argument to a method", removing those false positives.
Closes #24275.