mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:43 +00:00

## Summary Added checks for subscript expressions on builtin classes as in FURB189. The object is changed to use the collections objects and the types from the subscript are kept. Resolves #16130 > Note: Added some comments in the code explaining why ## Test Plan - Added a subscript dict and list class to the test file. - Tested locally to check that the symbols are changed and the types are kept. - No modifications changed on optional `str` values.
48 lines
891 B
Python
48 lines
891 B
Python
# setup
|
|
from enum import Enum, EnumMeta
|
|
from collections import UserList as UL
|
|
|
|
class SetOnceMappingMixin:
|
|
__slots__ = ()
|
|
def __setitem__(self, key, value):
|
|
if key in self:
|
|
raise KeyError(str(key) + ' already set')
|
|
return super().__setitem__(key, value)
|
|
|
|
|
|
class CaseInsensitiveEnumMeta(EnumMeta):
|
|
pass
|
|
|
|
# positives
|
|
class D(dict):
|
|
pass
|
|
|
|
class L(list):
|
|
pass
|
|
|
|
class S(str):
|
|
pass
|
|
|
|
class SubscriptDict(dict[str, str]):
|
|
pass
|
|
|
|
class SubscriptList(list[str]):
|
|
pass
|
|
|
|
# currently not detected
|
|
class SetOnceDict(SetOnceMappingMixin, dict):
|
|
pass
|
|
|
|
# negatives
|
|
class C:
|
|
pass
|
|
|
|
class I(int):
|
|
pass
|
|
|
|
class ActivityState(str, Enum, metaclass=CaseInsensitiveEnumMeta):
|
|
"""Activity state. This is an optional property and if not provided, the state will be Active by
|
|
default.
|
|
"""
|
|
ACTIVE = "Active"
|
|
INACTIVE = "Inactive"
|