Skip to content

Commit

Permalink
start to use Perflint and flake8-comprehensions to improved performance
Browse files Browse the repository at this point in the history
  • Loading branch information
07pepa committed Oct 28, 2024
1 parent 22e4acc commit cce73da
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 70 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.6.9
rev: v0.7.1
hooks:
- id: ruff
args: [ --fix ]
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.2
rev: v1.13.0
hooks:
- id: mypy
additional_dependencies:
Expand Down
5 changes: 1 addition & 4 deletions beanie/migrations/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,7 @@ async def build(cls, path: Path):
:return:
"""
logger.info("Building migration list")
names = []
for modulepath in path.glob("*.py"):
names.append(modulepath.name)
names.sort()
names = sorted([modulepath.name for modulepath in path.glob("*.py")])

db = DBHandler.get_db()
await init_beanie(
Expand Down
10 changes: 6 additions & 4 deletions beanie/odm/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,12 +1182,14 @@ async def fetch_link(self, field: Union[str, Any]):
setattr(self, field, values)

async def fetch_all_links(self):
coros = []
link_fields = self.get_link_fields()
if link_fields is not None:
for ref in link_fields.values():
coros.append(self.fetch_link(ref.field_name)) # TODO lists
await asyncio.gather(*coros)
await asyncio.gather(
*[
self.fetch_link(ref.field_name)
for ref in link_fields.values()
]
)

@classmethod
def get_link_fields(cls) -> Optional[Dict[str, LinkInfo]]:
Expand Down
13 changes: 3 additions & 10 deletions beanie/odm/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ async def fetch_list(
data = Link.repack_links(links) # type: ignore
ids_to_fetch = []
document_class = None
for doc_id, link in data.items():
for link in data.values():
if isinstance(link, Link):
if document_class is None:
document_class = link.document_class
Expand Down Expand Up @@ -363,10 +363,7 @@ def repack_links(

@classmethod
async def fetch_many(cls, links: List[Link]):
coros = []
for link in links:
coros.append(link.fetch())
return await asyncio.gather(*coros)
return await asyncio.gather(*[link.fetch() for link in links])

if IS_PYDANTIC_V2:

Expand Down Expand Up @@ -538,11 +535,7 @@ def __repr__(self):
def list_difference(
left: List[IndexModelField], right: List[IndexModelField]
):
result = []
for index in left:
if index not in right:
result.append(index)
return result
return [index for index in left if index not in right]

@staticmethod
def list_to_index_model(left: List[IndexModelField]):
Expand Down
3 changes: 1 addition & 2 deletions beanie/odm/interfaces/find.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,7 @@ def _add_class_id_filter(cls, args: Tuple, with_children: bool = False):
args += (
{
cls.get_settings().class_id: {
"$in": [cls._class_id]
+ [cname for cname in cls._children.keys()]
"$in": [cls._class_id] + list(cls._children.keys())
}
},
)
Expand Down
2 changes: 1 addition & 1 deletion beanie/odm/utils/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def set_default_class_vars(cls: Type[Document]):
to init settings
:return:
"""
cls._children = dict()
cls._children = {}
cls._parent = None
cls._inheritance_inited = False
cls._class_id = None
Expand Down
10 changes: 6 additions & 4 deletions beanie/odm/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ async def fetch_link(self, field: Union[str, Any]):
setattr(self, field, values)

async def fetch_all_links(self):
coros = []
link_fields = self.get_link_fields()
if link_fields is not None:
for ref in link_fields.values():
coros.append(self.fetch_link(ref.field_name)) # TODO lists
await asyncio.gather(*coros)
await asyncio.gather(
*[
self.fetch_link(ref.field_name)
for ref in link_fields.values()
]
)

@classmethod
def get_link_fields(cls) -> Optional[Dict[str, LinkInfo]]:
Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ include = ["**/*.py", ".github/**/*.py"]

[tool.ruff.lint]
ignore = ["E501"]
extend-select = ["I001"]
extend-select = [
'I', # isort
'PERF', # Perflint
'C4', # flake8-comprehensions
]
per-file-ignores = { "tests/*" = ["E711"] }

# Allow unused variables when underscore-prefixed.
Expand Down
2 changes: 1 addition & 1 deletion tests/fastapi/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async def create_houses_with_window_link(window: WindowInput):
HouseAPI.model_validate if IS_PYDANTIC_V2 else HouseAPI.parse_obj
)
house = validator(
dict(name="test_name", windows=[WindowAPI.link_from_id(window.id)])
{"name": "test_name", "windows": [WindowAPI.link_from_id(window.id)]}
)
await house.insert(link_rule=WriteRules.WRITE)
return house
Expand Down
14 changes: 12 additions & 2 deletions tests/migrations/test_break.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,17 @@ async def test_migration_break(settings, notes, db):
inspection = await OldNote.inspect_collection()
assert inspection.status == InspectionStatuses.OK
notes = await OldNote.get_motor_collection().find().to_list(length=100)
names = set(n["name"] for n in notes)
assert names == {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
assert {n["name"] for n in notes} == {
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
}
for note in notes:
assert "title" not in note
30 changes: 16 additions & 14 deletions tests/odm/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,16 @@ async def deprecated_init_beanie(db):
database=db,
document_models=[DocumentWithDeprecatedHiddenField],
)
assert len(w) == 1
assert issubclass(w[-1].category, DeprecationWarning)
assert (
"DocumentWithDeprecatedHiddenField: 'hidden=True' is deprecated, please use 'exclude=True'"
in str(w[-1].message)
)
found = False
for warning in w:
if (
"DocumentWithDeprecatedHiddenField: 'hidden=True' is deprecated, please use 'exclude=True"
in str(warning.message)
):
found = True
break

assert found


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -372,15 +376,13 @@ def document_soft_delete_not_inserted():

@pytest.fixture
def documents_soft_delete_not_inserted():
docs = []
for i in range(3):
docs.append(
DocumentTestModelWithSoftDelete(
test_int=randint(0, 1000000),
test_str="kipasa",
)
return [
DocumentTestModelWithSoftDelete(
test_int=randint(0, 1000000),
test_str="kipasa",
)
return docs
for _ in range(3)
]


@pytest.fixture
Expand Down
10 changes: 5 additions & 5 deletions tests/odm/documents/test_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ async def test_inheritance(self, db):
assert len(white_vehicles) == 3
assert len(cars_only) == 2

assert {Car, Bus} == set(i.__class__ for i in cars_and_buses)
assert {Bicycle, Car, Bus} == set(i.__class__ for i in white_vehicles)
assert {Car, Bus} == {i.__class__ for i in cars_and_buses}
assert {Bicycle, Car, Bus} == {i.__class__ for i in white_vehicles}

white_vehicles_2 = await Car.find(Vehicle.color == "white").to_list()
assert len(white_vehicles_2) == 1
Expand Down Expand Up @@ -92,13 +92,13 @@ async def test_links(self, db):

# re-fetch from DB w/o links
owner = await Owner.get(owner.id)
assert {Link} == set(i.__class__ for i in owner.vehicles)
assert {Link} == {i.__class__ for i in owner.vehicles}
await owner.fetch_all_links()
assert {Car, Bus} == set(i.__class__ for i in owner.vehicles)
assert {Car, Bus} == {i.__class__ for i in owner.vehicles}

# re-fetch from DB with resolved links
owner = await Owner.get(owner.id, fetch_links=True)
assert {Car, Bus} == set(i.__class__ for i in owner.vehicles)
assert {Car, Bus} == {i.__class__ for i in owner.vehicles}

for e in (owner, car_1, car_2, bus_1):
await e.delete()
Expand Down
9 changes: 4 additions & 5 deletions tests/odm/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,10 @@ async def test_aggregation(documents):

async def test_capacity(documents):
await documents(10)
docs = []
for i in range(10):
docs.append(
await DocumentTestModel.find_one(DocumentTestModel.test_int == i)
)
docs = [
await DocumentTestModel.find_one(DocumentTestModel.test_int == i)
for i in range(10)
]

await DocumentTestModel.find_one(DocumentTestModel.test_int == 1).set(
{DocumentTestModel.test_str: "NEW_VALUE"}
Expand Down
2 changes: 1 addition & 1 deletion tests/odm/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async def test_custom_filed_types():
ipv6network="2001:db00::0/24",
timedelta=4782453,
set_type={"one", "two", "three"},
tuple_type=tuple([3, "string"]),
tuple_type=(3, "string"),
path="/etc/hosts",
)
custom2 = DocumentWithCustomFiledsTypes(
Expand Down
11 changes: 5 additions & 6 deletions tests/odm/test_state_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,12 @@ async def test_find_one(self, saved_doc_default, state):
assert new_doc.get_previous_saved_state() is None

async def test_find_many(self):
docs = []
for i in range(10):
docs.append(
DocumentWithTurnedOnStateManagement(
num_1=i, num_2=i + 1, internal=InternalDoc()
)
docs = [
DocumentWithTurnedOnStateManagement(
num_1=i, num_2=i + 1, internal=InternalDoc()
)
for i in range(10)
]
await DocumentWithTurnedOnStateManagement.insert_many(docs)

found_docs = await DocumentWithTurnedOnStateManagement.find(
Expand Down
13 changes: 5 additions & 8 deletions tests/typing/find.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ async def find_many_with_projection() -> List[ProjectionTest]:


async def find_many_generator() -> List[Test]:
docs: List[Test] = []
async for doc in Test.find():
docs.append(doc)
return docs
return [doc async for doc in Test.find()]


async def find_many_generator_with_projection() -> List[ProjectionTest]:
docs: List[ProjectionTest] = []
async for doc in Test.find().project(projection_model=ProjectionTest):
docs.append(doc)
return docs
return [
doc
async for doc in Test.find().project(projection_model=ProjectionTest)
]


async def find_one() -> Optional[Test]:
Expand Down

0 comments on commit cce73da

Please sign in to comment.