From c880fcf787d7f96a862f17cbd599130a573ca105 Mon Sep 17 00:00:00 2001 From: Mathieu Kniewallner Date: Sun, 22 May 2022 01:00:04 +0200 Subject: [PATCH 1/3] chore(pre-commit): bump flake8 additional dependencies --- .pre-commit-config.yaml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f462275abab..ad814c7efc9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,17 +31,17 @@ repos: hooks: - id: yesqa additional_dependencies: &flake8_deps - - flake8-annotations==2.7.0 + - flake8-annotations==2.9.0 - flake8-broken-line==0.4.0 - - flake8-bugbear==21.9.2 - - flake8-comprehensions==3.7.0 - - flake8-eradicate==1.2.0 - - flake8-no-pep420==1.2.0 + - flake8-bugbear==22.4.25 + - flake8-comprehensions==3.10.0 + - flake8-eradicate==1.2.1 + - flake8-no-pep420==2.3.0 - flake8-quotes==3.3.1 - - flake8-simplify==0.14.2 - - flake8-tidy-imports==4.5.0 - - flake8-type-checking==1.1.0 - - flake8-typing-imports==1.11.0 + - flake8-simplify==0.19.2 + - flake8-tidy-imports==4.8.0 + - flake8-type-checking==1.5.0 + - flake8-typing-imports==1.12.0 - flake8-use-fstring==1.3 - pep8-naming==0.12.1 From b1a6ef5b63f36d17f69e2487085d6d33f860601a Mon Sep 17 00:00:00 2001 From: Mathieu Kniewallner Date: Sun, 22 May 2022 01:02:12 +0200 Subject: [PATCH 2/3] refactor: remove unnecessary maps to respect `C417` --- src/poetry/console/commands/source/show.py | 2 +- src/poetry/utils/authenticator.py | 2 +- tests/console/commands/source/test_show.py | 21 +++++++++------------ 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/poetry/console/commands/source/show.py b/src/poetry/console/commands/source/show.py index 8ed4b360ec9..1d5b3044f18 100644 --- a/src/poetry/console/commands/source/show.py +++ b/src/poetry/console/commands/source/show.py @@ -26,7 +26,7 @@ def handle(self) -> int | None: self.line("No sources configured for this project.") return 0 - if names and not any(map(lambda s: s.name in names, sources)): + if names and not any(s.name in names for s in sources): self.line_error(f"No source found with name(s): {', '.join(names)}") return 1 diff --git a/src/poetry/utils/authenticator.py b/src/poetry/utils/authenticator.py index d460aee555b..006cc753362 100644 --- a/src/poetry/utils/authenticator.py +++ b/src/poetry/utils/authenticator.py @@ -384,7 +384,7 @@ def get_repository_config_for_url( logger.debug( "Multiple source configurations found for %s - %s", parsed_url.netloc, - ", ".join(map(lambda c: c.name, candidates)), + ", ".join(c.name for c in candidates), ) # prefer the more specific path candidates.sort( diff --git a/tests/console/commands/source/test_show.py b/tests/console/commands/source/test_show.py index 78c0c6f7a00..8b975cf4b92 100644 --- a/tests/console/commands/source/test_show.py +++ b/tests/console/commands/source/test_show.py @@ -41,10 +41,9 @@ def test_source_show_simple(tester: CommandTester): default : no secondary : no """.splitlines() - assert ( - list(map(lambda l: l.strip(), tester.io.fetch_output().strip().splitlines())) - == expected - ) + assert [ + line.strip() for line in tester.io.fetch_output().strip().splitlines() + ] == expected assert tester.status_code == 0 @@ -57,10 +56,9 @@ def test_source_show_one(tester: CommandTester, source_one: Source): default : no secondary : no """.splitlines() - assert ( - list(map(lambda l: l.strip(), tester.io.fetch_output().strip().splitlines())) - == expected - ) + assert [ + line.strip() for line in tester.io.fetch_output().strip().splitlines() + ] == expected assert tester.status_code == 0 @@ -78,10 +76,9 @@ def test_source_show_two(tester: CommandTester, source_one: Source, source_two: default : no secondary : no """.splitlines() - assert ( - list(map(lambda l: l.strip(), tester.io.fetch_output().strip().splitlines())) - == expected - ) + assert [ + line.strip() for line in tester.io.fetch_output().strip().splitlines() + ] == expected assert tester.status_code == 0 From 356766efaca264ddba3484dc2b00ee811079326d Mon Sep 17 00:00:00 2001 From: Mathieu Kniewallner Date: Sun, 22 May 2022 12:21:46 +0200 Subject: [PATCH 3/3] refactor: ensure garbage collection of objects using lru cache Decorating class instances methods using `lru_cache` prevents garbage collection to happen when an object gets deleted. This could be really bad for memory consumption. Some resources about this: - https://rednafi.github.io/reflections/dont-wrap-instance-methods-with-functoolslru_cache-decorator-in-python.html - https://www.youtube.com/watch?v=sVjtp6tGo0g Originally spotted by rule `B019` of `flake8-bugbear`. --- src/poetry/mixology/term.py | 8 ++++---- src/poetry/utils/authenticator.py | 6 ++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/poetry/mixology/term.py b/src/poetry/mixology/term.py index d19a190024d..6e75158ebbe 100644 --- a/src/poetry/mixology/term.py +++ b/src/poetry/mixology/term.py @@ -23,6 +23,8 @@ class Term: def __init__(self, dependency: Dependency, is_positive: bool) -> None: self._dependency = dependency self._positive = is_positive + self.relation = functools.lru_cache(maxsize=None)(self._relation) + self.intersect = functools.lru_cache(maxsize=None)(self._intersect) @property def inverse(self) -> Term: @@ -48,8 +50,7 @@ def satisfies(self, other: Term) -> bool: and self.relation(other) == SetRelation.SUBSET ) - @functools.lru_cache(maxsize=None) - def relation(self, other: Term) -> str: + def _relation(self, other: Term) -> str: """ Returns the relationship between the package versions allowed by this term and another. @@ -111,8 +112,7 @@ def relation(self, other: Term) -> str: # not foo ^1.5.0 is a superset of not foo ^1.0.0 return SetRelation.OVERLAPPING - @functools.lru_cache(maxsize=None) - def intersect(self, other: Term) -> Term | None: + def _intersect(self, other: Term) -> Term | None: """ Returns a Term that represents the packages allowed by both this term and another diff --git a/src/poetry/utils/authenticator.py b/src/poetry/utils/authenticator.py index 006cc753362..11b9aa68dfe 100644 --- a/src/poetry/utils/authenticator.py +++ b/src/poetry/utils/authenticator.py @@ -104,6 +104,9 @@ def __init__( if not disable_cache else None ) + self.get_repository_config_for_url = functools.lru_cache(maxsize=None)( + self._get_repository_config_for_url + ) @property def cache(self) -> FileCache | None: @@ -351,8 +354,7 @@ def get_certs_for_url(self, url: str) -> dict[str, Path | None]: self._certs[url] = self._get_certs_for_url(url) return self._certs[url] - @functools.lru_cache(maxsize=None) - def get_repository_config_for_url( + def _get_repository_config_for_url( self, url: str, exact_match: bool = False ) -> AuthenticatorRepositoryConfig | None: parsed_url = urllib.parse.urlsplit(url)