Add flake8-bandit linting (#156)

The linter has been used in the past, so most assertions for these were
already there but needed to be updated to use `noqa: S` instead of
`nosec: B` annotations.
This commit is contained in:
Martijn Pieters
2023-05-16 13:11:10 +01:00
committed by GitHub
parent cbad93c970
commit 50e3f91a87
5 changed files with 13 additions and 9 deletions

View File

@@ -59,12 +59,12 @@ class FastAPICache:
@classmethod @classmethod
def get_backend(cls) -> Backend: def get_backend(cls) -> Backend:
assert cls._backend, "You must call init first!" # nosec: B101 assert cls._backend, "You must call init first!" # noqa: S101
return cls._backend return cls._backend
@classmethod @classmethod
def get_prefix(cls) -> str: def get_prefix(cls) -> str:
assert cls._prefix is not None, "You must call init first!" # nosec: B101 assert cls._prefix is not None, "You must call init first!" # noqa: S101
return cls._prefix return cls._prefix
@classmethod @classmethod
@@ -73,17 +73,17 @@ class FastAPICache:
@classmethod @classmethod
def get_coder(cls) -> Type[Coder]: def get_coder(cls) -> Type[Coder]:
assert cls._coder, "You must call init first!" # nosec: B101 assert cls._coder, "You must call init first!" # noqa: S101
return cls._coder return cls._coder
@classmethod @classmethod
def get_key_builder(cls) -> KeyBuilder: def get_key_builder(cls) -> KeyBuilder:
assert cls._key_builder, "You must call init first!" # nosec: B101 assert cls._key_builder, "You must call init first!" # noqa: S101
return cls._key_builder return cls._key_builder
@classmethod @classmethod
def get_cache_status_header(cls) -> str: def get_cache_status_header(cls) -> str:
assert cls._cache_status_header, "You must call init first!" # nosec: B101 assert cls._cache_status_header, "You must call init first!" # noqa: S101
return cls._cache_status_header return cls._cache_status_header
@classmethod @classmethod
@@ -92,6 +92,6 @@ class FastAPICache:
@classmethod @classmethod
async def clear(cls, namespace: Optional[str] = None, key: Optional[str] = None) -> int: async def clear(cls, namespace: Optional[str] = None, key: Optional[str] = None) -> int:
assert cls._backend and cls._prefix is not None, "You must call init first!" # nosec: B101 assert cls._backend and cls._prefix is not None, "You must call init first!" # noqa: S101
namespace = cls._prefix + (":" + namespace if namespace else "") namespace = cls._prefix + (":" + namespace if namespace else "")
return await cls._backend.clear(namespace, key) return await cls._backend.clear(namespace, key)

View File

@@ -128,7 +128,7 @@ class PickleCoder(Coder):
@classmethod @classmethod
def decode(cls, value: bytes) -> Any: def decode(cls, value: bytes) -> Any:
return pickle.loads(value) # nosec:B403,B301 return pickle.loads(value) # noqa: S301
@classmethod @classmethod
def decode_as_type(cls, value: bytes, *, type_: Optional[_T]) -> Any: def decode_as_type(cls, value: bytes, *, type_: Optional[_T]) -> Any:

View File

@@ -171,7 +171,7 @@ def cache(
) )
if isawaitable(cache_key): if isawaitable(cache_key):
cache_key = await cache_key cache_key = await cache_key
assert isinstance(cache_key, str) assert isinstance(cache_key, str) # noqa: S101 # assertion is a type guard
try: try:
ttl, cached = await backend.get_with_ttl(cache_key) ttl, cached = await backend.get_with_ttl(cache_key)

View File

@@ -14,7 +14,7 @@ def default_key_builder(
args: Tuple[Any, ...], args: Tuple[Any, ...],
kwargs: Dict[str, Any], kwargs: Dict[str, Any],
) -> str: ) -> str:
cache_key = hashlib.md5( # nosec:B303 cache_key = hashlib.md5( # noqa: S324
f"{func.__module__}:{func.__name__}:{args}:{kwargs}".encode() f"{func.__module__}:{func.__name__}:{args}:{kwargs}".encode()
).hexdigest() ).hexdigest()
return f"{namespace}:{cache_key}" return f"{namespace}:{cache_key}"

View File

@@ -85,11 +85,15 @@ select = [
"E", # pycodestyle errors "E", # pycodestyle errors
"F", # pyflakes "F", # pyflakes
"I", # isort "I", # isort
"S", # flake8-bandit
"W", # pycodestyle warnings "W", # pycodestyle warnings
"UP", # pyupgrade "UP", # pyupgrade
] ]
target-version = "py37" target-version = "py37"
[tool.ruff.per-file-ignores]
"tests/**/*.py" = ["S101"]
[build-system] [build-system]
requires = ["poetry-core"] requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api" build-backend = "poetry.core.masonry.api"