Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ruff for imports and formatting #317

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: ruff, isort, black and mypy checks
name: ruff and mypy checks

on:
push:
Expand Down Expand Up @@ -27,27 +27,19 @@ jobs:

- name: Ruff Linting AstraPy
run: |
poetry run ruff astrapy
poetry run ruff check astrapy

- name: Ruff Linting Tests
run: |
poetry run ruff tests
poetry run ruff check tests

- name: Isort Linting AstraPy
- name: Ruff formatting astrapy
run: |
poetry run isort --check astrapy --profile black

- name: Isort Linting Tests
run: |
poetry run isort --check tests --profile black

- name: Black linting astrapy
run: |
poetry run black --check astrapy
poetry run ruff format --check astrapy

- name: Black linting tests
- name: Ruff formatting tests
run: |
poetry run black --check tests
poetry run ruff format --check tests

- name: Run MyPy AstraPy
run: |
Expand Down
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
repos:
- repo: https://github.com/psf/black
rev: 24.2.0 # Replace with your desired version or use 'stable'
hooks:
- id: black
args: ['--quiet']

- repo: local
hooks:
- id: check
name: check
language: system
entry: make format-fix
10 changes: 4 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ FMT_FLAGS ?= --check
format: format-src format-tests

format-tests:
poetry run ruff tests
poetry run isort tests $(FMT_FLAGS) --profile black
poetry run black tests $(FMT_FLAGS)
poetry run ruff check tests
poetry run ruff format tests $(FMT_FLAGS)
poetry run mypy tests

format-src:
poetry run ruff astrapy
poetry run isort astrapy $(FMT_FLAGS) --profile black
poetry run black astrapy $(FMT_FLAGS)
poetry run ruff check astrapy
poetry run ruff format astrapy $(FMT_FLAGS)
poetry run mypy astrapy

format-fix: format-fix-src format-fix-tests
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ Linter, style and typecheck should all pass for a PR:
make format
```

With `make format-fix` the style and imports are autofixed (by `black` and `isort` resp.)
With `make format-fix` the style and imports are autofixed (by `ruff`)

Features must be thoroughly covered in tests (see `tests/idiomatic/*` for
naming convention and module structure).
Expand Down
3 changes: 1 addition & 2 deletions astrapy/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ def insert_many(
with ThreadPoolExecutor(max_workers=_concurrency) as executor:

def _chunk_insertor(
document_chunk: List[Dict[str, Any]]
document_chunk: List[Dict[str, Any]],
) -> Dict[str, Any]:
im_payload = {
"insertMany": {
Expand Down Expand Up @@ -5397,7 +5397,6 @@ async def bulk_write(
logger.info(f"finished a bulk write on '{self.name}'")
return full_bw_result
else:

sem = asyncio.Semaphore(_concurrency)

async def _concurrent_execute_as_either(
Expand Down
8 changes: 4 additions & 4 deletions astrapy/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def is_list_of_floats(vector: Iterable[Any]) -> bool:


def convert_to_ejson_date_object(
date_value: Union[datetime.date, datetime.datetime]
date_value: Union[datetime.date, datetime.datetime],
) -> Dict[str, int]:
return {"$date": int(time.mktime(date_value.timetuple()) * 1000)}

Expand All @@ -355,7 +355,7 @@ def convert_to_ejson_objectid_object(objectid_value: ObjectId) -> Dict[str, str]


def convert_ejson_date_object_to_datetime(
date_object: Dict[str, int]
date_object: Dict[str, int],
) -> datetime.datetime:
return datetime.datetime.fromtimestamp(date_object["$date"] / 1000.0)

Expand All @@ -365,7 +365,7 @@ def convert_ejson_uuid_object_to_uuid(uuid_object: Dict[str, str]) -> UUID:


def convert_ejson_objectid_object_to_objectid(
objectid_object: Dict[str, str]
objectid_object: Dict[str, str],
) -> ObjectId:
return ObjectId(objectid_object["$objectId"])

Expand Down Expand Up @@ -402,7 +402,7 @@ def _normalize_payload_value(path: List[str], value: Any) -> Any:


def normalize_for_api(
payload: Union[Dict[str, Any], None]
payload: Union[Dict[str, Any], None],
) -> Union[Dict[str, Any], None]:
"""
Normalize a payload for API calls.
Expand Down
1 change: 0 additions & 1 deletion astrapy/cursors.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def _maybe_valid_list_index(key_block: str) -> Optional[int]:
def _create_document_key_extractor(
key: str,
) -> Callable[[Dict[str, Any]], Iterable[Any]]:

key_blocks0: List[IndexPairType] = [
(kb_str, _maybe_valid_list_index(kb_str)) for kb_str in key.split(".")
]
Expand Down
6 changes: 3 additions & 3 deletions astrapy/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def as_dict(self) -> Dict[str, Any]:

@staticmethod
def from_dict(
raw_dict: Optional[Dict[str, Any]]
raw_dict: Optional[Dict[str, Any]],
) -> Optional[CollectionDefaultIDOptions]:
"""
Create an instance of CollectionDefaultIDOptions from a dictionary
Expand Down Expand Up @@ -201,7 +201,7 @@ def as_dict(self) -> Dict[str, Any]:

@staticmethod
def from_dict(
raw_dict: Optional[Dict[str, Any]]
raw_dict: Optional[Dict[str, Any]],
) -> Optional[CollectionVectorServiceOptions]:
"""
Create an instance of CollectionVectorServiceOptions from a dictionary
Expand Down Expand Up @@ -252,7 +252,7 @@ def as_dict(self) -> Dict[str, Any]:

@staticmethod
def from_dict(
raw_dict: Optional[Dict[str, Any]]
raw_dict: Optional[Dict[str, Any]],
) -> Optional[CollectionVectorOptions]:
"""
Create an instance of CollectionVectorOptions from a dictionary
Expand Down
8 changes: 4 additions & 4 deletions astrapy/transform_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def is_list_of_floats(vector: Iterable[Any]) -> bool:


def convert_to_ejson_date_object(
date_value: Union[datetime.date, datetime.datetime]
date_value: Union[datetime.date, datetime.datetime],
) -> Dict[str, int]:
return {"$date": int(time.mktime(date_value.timetuple()) * 1000)}

Expand All @@ -60,7 +60,7 @@ def convert_to_ejson_objectid_object(objectid_value: ObjectId) -> Dict[str, str]


def convert_ejson_date_object_to_datetime(
date_object: Dict[str, int]
date_object: Dict[str, int],
) -> datetime.datetime:
return datetime.datetime.fromtimestamp(date_object["$date"] / 1000.0)

Expand All @@ -70,7 +70,7 @@ def convert_ejson_uuid_object_to_uuid(uuid_object: Dict[str, str]) -> UUID:


def convert_ejson_objectid_object_to_objectid(
objectid_object: Dict[str, str]
objectid_object: Dict[str, str],
) -> ObjectId:
return ObjectId(objectid_object["$objectId"])

Expand Down Expand Up @@ -105,7 +105,7 @@ def normalize_payload_value(path: List[str], value: Any) -> Any:


def normalize_for_api(
payload: Union[Dict[str, Any], None]
payload: Union[Dict[str, Any], None],
) -> Union[Dict[str, Any], None]:
"""
Normalize a payload for API calls.
Expand Down
2 changes: 1 addition & 1 deletion astrapy/user_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def compose_user_agent_string(


def compose_full_user_agent(
callers: List[Tuple[Optional[str], Optional[str]]]
callers: List[Tuple[Optional[str], Optional[str]]],
) -> Optional[str]:
user_agent_strings = [
ua_string
Expand Down
112 changes: 21 additions & 91 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pymongo = ">=3"
httpx = { version=">=0.25.2,<1", extras=["http2"] }

[tool.poetry.group.dev.dependencies]
black = "~24.3.0"
faker = "~23.1.0"
mypy = "~1.9.0"
pre-commit = "~3.5.0"
Expand All @@ -47,14 +46,16 @@ pytest = "~8.0.0"
python-dotenv = "~1.0.1"
pytest-httpserver = "~1.0.8"
testcontainers = "~3.7.1"
ruff = "~0.2.1"
ruff = "^0.6.8"
types-toml = "^0.10.8.7"
isort = "^5.13.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.ruff.lint]
select = ["E4", "E7", "E9", "F", "FA", "I"]

[tool.mypy]
disallow_any_generics = true
disallow_incomplete_defs = true
Expand Down
Loading
Loading