Skip to content

Commit

Permalink
Use ruff for linting (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
jowilf authored Nov 26, 2022
1 parent 5ee62a9 commit 079d162
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 47 deletions.
58 changes: 25 additions & 33 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,30 @@ classifiers = [

[tool.poetry.dependencies]
python = "^3.7"
SQLAlchemy = ">=1.4,<1.5.0"
SQLAlchemy = ">=1.4,<1.5"
apache-libcloud = ">=3.6.0,<3.7"

[tool.poetry.dev-dependencies]
pytest = "^7.1.2"
pytest = "^7.2.0"
mypy = "0.991"
black = "22.10.0"
ruff = "0.0.139"
sqlmodel = "^0.0.8"
Pillow = "^9.2.0"
Pillow = "^9.3.0"
fasteners = "^0.18"
black = "^22.6.0"
coverage = { extras = ["toml"], version = "^6.4.2" }
flake8 = "^3.9.2"
mypy = "^0.982"
isort = "^5.10.1"
mkdocs-material = "^8.4.3"
coverage = { extras = ["toml"], version = "^6.5.0" }
mkdocs-material = "^8.5.10"
PyMySQL = { extras = ["rsa"], version = "^1.0.2" }
psycopg2-binary = "^2.9.3"
psycopg2-binary = "^2.9.5"
mkdocstrings = { extras = ["python"], version = "^0.19.0" }
fastapi = "^0.85.1"
uvicorn = "^0.18.2"
fastapi = "^0.87.0"
uvicorn = "^0.20.0"
python-multipart = "^0.0.5"
Flask = "^2.2.2"
Flask-SQLAlchemy = "^2.5.1"
importlib-metadata = { version = "<5.0", python = "<=3.7" }


[tool.coverage.report]
fail_under = 95
show_missing = true
Expand All @@ -65,30 +65,22 @@ parallel = true
command_line = "-m pytest"
source = ["sqlalchemy_file", "tests"]

[tool.isort]
profile = "black"
known_third_party = ["sqlalchemy_file"]
skip_glob = [
"sqlalchemy_file/__init__.py",
[tool.ruff]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"I", # isort
"C", # flake8-comprehensions
"B", # flake8-bugbear
]
src_paths = ["sqlalchemy_file", "tests"]
ignore = ["E501", "B904"]

[tool.ruff.isort]
known-third-party = ["sqlalchemy_file"]

[tool.mypy]
disallow_any_generics = true
disallow_subclassing_any = true
disallow_untyped_calls = false
disallow_untyped_defs = true
disallow_incomplete_defs = true
check_untyped_defs = true
disallow_untyped_decorators = true
no_implicit_optional = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_return_any = true
implicit_reexport = false
strict_equality = true
ignore_missing_imports = true
exclude = 'tests/'
strict = true


[build-system]
Expand Down
6 changes: 6 additions & 0 deletions scripts/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

set -e
set -x
ruff sqlalchemy_file tests --fix
black .
5 changes: 2 additions & 3 deletions scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ set -e
set -x

mypy sqlalchemy_file
flake8 sqlalchemy_file tests docs_src
black sqlalchemy_file tests docs_src --check
isort sqlalchemy_file tests docs_src --check-only
ruff sqlalchemy_file tests
black sqlalchemy_file tests --check
8 changes: 4 additions & 4 deletions sqlalchemy_file/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def get_metadata_file_obj(metadata: Dict[str, Any]) -> "SpooledTemporaryFile[byt
return f


def get_content_from_file_obj(fileobj): # type: ignore
def get_content_from_file_obj(fileobj: Any) -> Any:
"""Provides a real file object from file content
Converts ``str`` and ``bytes`` to an actual file.
"""
Expand All @@ -31,21 +31,21 @@ def get_content_from_file_obj(fileobj): # type: ignore
return fileobj


def get_filename_from_fileob(fileobj): # type: ignore
def get_filename_from_fileob(fileobj: Any) -> Any:
if getattr(fileobj, "filename", None) is not None:
return fileobj.filename
elif getattr(fileobj, "name", None) is not None:
return os.path.basename(fileobj.name)
return "unnamed"


def get_content_type_from_fileobj(fileobj, filename): # type: ignore
def get_content_type_from_fileobj(fileobj: Any, filename: str) -> Any:
if getattr(fileobj, "content_type", None) is not None:
return fileobj.content_type
return mimetypes.guess_type(filename, strict=False)[0] or "application/octet-stream"


def get_content_size_from_fileobj(file): # type: ignore
def get_content_size_from_fileobj(file: Any) -> Any:
if hasattr(file, "size"):
return file.size
if hasattr(file, "name"):
Expand Down
2 changes: 1 addition & 1 deletion sqlalchemy_file/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def __init__(
self.thumbnail_format = thumbnail_format

def process(self, file: "File", upload_storage: Optional[str] = None) -> None:
from PIL import Image
from PIL import Image # type: ignore

content = file.original_content
img = Image.open(content)
Expand Down
6 changes: 3 additions & 3 deletions sqlalchemy_file/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def __init__(


class FileFieldSessionTracker(object):
mapped_entities: Dict[Type[Any], List[str]] = dict()
mapped_entities: Dict[Type[Any], List[str]] = {}

@classmethod
def delete_files(cls, paths: Set[str], ctx: str) -> None:
Expand All @@ -162,9 +162,9 @@ def delete_files(cls, paths: Set[str], ctx: str) -> None:
@classmethod
def clear_session(cls, session: Session) -> None:
if hasattr(session, "_new_files"):
del session._new_files # type: ignore
del session._new_files
if hasattr(session, "_old_files"):
del session._old_files # type: ignore
del session._old_files

@classmethod
def add_new_files_to_session(cls, session: Session, paths: List[str]) -> None:
Expand Down
4 changes: 2 additions & 2 deletions sqlalchemy_file/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ def __init__(
max_aspect_ratio: Optional[float] = None,
allowed_content_types: Optional[List[str]] = None,
):
from PIL import Image
from PIL import Image # type: ignore

Image.init()
super().__init__(
allowed_content_types
if allowed_content_types is not None
else [type for type in Image.MIME.values()]
else list(Image.MIME.values())
)
self.min_width, self.min_height = min_wh if min_wh else (None, None)
self.max_width, self.max_height = max_wh if max_wh else (None, None)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_sqlmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Attachment(SQLModel, table=True):
__tablename__ = "attachment"

id: int = Field(None, primary_key=True)
name: str = Field(..., sa_column_kwargs=dict(unique=True))
name: str = Field(..., sa_column_kwargs={"unique": True})
content: Any = Field(sa_column=Column(FileField))


Expand Down

0 comments on commit 079d162

Please sign in to comment.