From 6732bb4bf884195df6d9f669f65f6e559a62db16 Mon Sep 17 00:00:00 2001 From: Edwin Yllanes Date: Wed, 2 Mar 2022 05:09:59 +0000 Subject: [PATCH 1/8] chore(utils): remove type errors --- pyproject.toml | 1 - src/poetry/utils/appdirs.py | 12 +++--------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6d46412174a..d5dad061520 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -131,7 +131,6 @@ module = [ 'poetry.mixology.version_solver', 'poetry.packages.locker', 'poetry.repositories.installed_repository', - 'poetry.utils.appdirs', 'poetry.utils.authenticator', 'poetry.utils.env', 'poetry.utils.exporter', diff --git a/src/poetry/utils/appdirs.py b/src/poetry/utils/appdirs.py index ca6bb9a432d..476bd0231fa 100644 --- a/src/poetry/utils/appdirs.py +++ b/src/poetry/utils/appdirs.py @@ -7,17 +7,11 @@ import os import sys -from typing import TYPE_CHECKING - - -if TYPE_CHECKING: - from pathlib import Path - WINDOWS = sys.platform.startswith("win") or (sys.platform == "cli" and os.name == "nt") -def expanduser(path: str | Path) -> str: +def expanduser(path: str) -> str: """ Expand ~ and ~user constructions. @@ -214,14 +208,14 @@ def _get_win_folder_with_ctypes(csidl_name: str) -> str: }[csidl_name] buf = ctypes.create_unicode_buffer(1024) - ctypes.windll.shell32.SHGetFolderPathW(None, csidl_const, None, 0, buf) + ctypes.windll.shell32.SHGetFolderPathW(None, csidl_const, None, 0, buf) # type: ignore[attr-defined] # noqa: E501 # Downgrade to short path name if have highbit chars. See # . has_high_char = any(ord(c) > 255 for c in buf) if has_high_char: buf2 = ctypes.create_unicode_buffer(1024) - if ctypes.windll.kernel32.GetShortPathNameW(buf.value, buf2, 1024): + if ctypes.windll.kernel32.GetShortPathNameW(buf.value, buf2, 1024): # type: ignore[attr-defined] # noqa: E501 buf = buf2 return buf.value From 90ac4c66ab86df25df23e0eea6daa66f0e37bc2a Mon Sep 17 00:00:00 2001 From: Edwin Yllanes Date: Wed, 2 Mar 2022 06:42:58 +0000 Subject: [PATCH 2/8] chore(utils/exporter): remove type errors --- pyproject.toml | 1 - src/poetry/utils/exporter.py | 11 ++++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index d5dad061520..8c0a8c593fa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -133,7 +133,6 @@ module = [ 'poetry.repositories.installed_repository', 'poetry.utils.authenticator', 'poetry.utils.env', - 'poetry.utils.exporter', 'poetry.utils.password_manager', 'poetry.utils.setup_reader', ] diff --git a/src/poetry/utils/exporter.py b/src/poetry/utils/exporter.py index 147b930fec2..2ba39d84190 100644 --- a/src/poetry/utils/exporter.py +++ b/src/poetry/utils/exporter.py @@ -6,16 +6,16 @@ from typing import TYPE_CHECKING from typing import Sequence +from cleo.io.io import IO from poetry.core.packages.utils.utils import path_to_url +from poetry.repositories.remote_repository import RemoteRepository from poetry.utils._compat import decode if TYPE_CHECKING: from pathlib import Path - from cleo.io.io import IO - from poetry.poetry import Poetry @@ -143,7 +143,7 @@ def _export_requirements_txt( repositories = [ r for r in self._poetry.pool.repositories - if r.url == index.rstrip("/") + if (isinstance(r, RemoteRepository) and r.url == index.rstrip("/")) ] if not repositories: continue @@ -151,6 +151,7 @@ def _export_requirements_txt( if ( self._poetry.pool.has_default() and repository is self._poetry.pool.repositories[0] + and isinstance(repository, RemoteRepository) ): url = ( repository.authenticated_url @@ -174,9 +175,9 @@ def _export_requirements_txt( def _output(self, content: str, cwd: Path, output: IO | str) -> None: decoded = decode(content) - try: + if isinstance(output, IO): output.write(decoded) - except AttributeError: + else: filepath = cwd / output with filepath.open("w", encoding="utf-8") as f: f.write(decoded) From 323ab248e4943f8376ea468d1ccc5017a87adb71 Mon Sep 17 00:00:00 2001 From: Edwin Yllanes Date: Wed, 2 Mar 2022 06:56:45 +0000 Subject: [PATCH 3/8] chore(utils/exporter): remove type errors --- src/poetry/utils/exporter.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/poetry/utils/exporter.py b/src/poetry/utils/exporter.py index 2ba39d84190..8e10770f303 100644 --- a/src/poetry/utils/exporter.py +++ b/src/poetry/utils/exporter.py @@ -1,12 +1,12 @@ from __future__ import annotations +import contextlib import itertools import urllib.parse from typing import TYPE_CHECKING from typing import Sequence -from cleo.io.io import IO from poetry.core.packages.utils.utils import path_to_url from poetry.repositories.remote_repository import RemoteRepository @@ -16,6 +16,8 @@ if TYPE_CHECKING: from pathlib import Path + from cleo.io.io import IO + from poetry.poetry import Poetry @@ -175,9 +177,11 @@ def _export_requirements_txt( def _output(self, content: str, cwd: Path, output: IO | str) -> None: decoded = decode(content) - if isinstance(output, IO): - output.write(decoded) - else: + if isinstance(output, str): filepath = cwd / output with filepath.open("w", encoding="utf-8") as f: f.write(decoded) + return + with contextlib.suppress(AttributeError): + # FIXME + output.write(decoded) From fd505d38b3d60526ac0ea5d2ce2c8d29a41182d8 Mon Sep 17 00:00:00 2001 From: Edwin Yllanes Date: Wed, 2 Mar 2022 14:27:57 +0000 Subject: [PATCH 4/8] chore(utils/exporter): remove type errors --- src/poetry/utils/exporter.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/poetry/utils/exporter.py b/src/poetry/utils/exporter.py index 8e10770f303..209c7d9a9d4 100644 --- a/src/poetry/utils/exporter.py +++ b/src/poetry/utils/exporter.py @@ -1,6 +1,5 @@ from __future__ import annotations -import contextlib import itertools import urllib.parse @@ -15,11 +14,14 @@ if TYPE_CHECKING: from pathlib import Path - - from cleo.io.io import IO + from typing import Protocol from poetry.poetry import Poetry + class Writer(Protocol): + def write(self, text: str) -> None: + pass + class Exporter: """ @@ -38,7 +40,7 @@ def export( self, fmt: str, cwd: Path, - output: IO | str, + output: Writer | str, with_hashes: bool = True, dev: bool = False, extras: bool | Sequence[str] | None = None, @@ -61,7 +63,7 @@ def export( def _export_requirements_txt( self, cwd: Path, - output: IO | str, + output: Writer | str, with_hashes: bool = True, dev: bool = False, extras: bool | Sequence[str] | None = None, @@ -173,15 +175,13 @@ def _export_requirements_txt( content = indexes_header + "\n" + content - self._output(content, cwd, output) + self._write(content, cwd, output) - def _output(self, content: str, cwd: Path, output: IO | str) -> None: + def _write(self, content: str, cwd: Path, output: Writer | str) -> None: decoded = decode(content) if isinstance(output, str): filepath = cwd / output with filepath.open("w", encoding="utf-8") as f: f.write(decoded) return - with contextlib.suppress(AttributeError): - # FIXME - output.write(decoded) + output.write(decoded) From ff0ed6fe14119ac352da49c816fa1f9547b85218 Mon Sep 17 00:00:00 2001 From: Edwin Yllanes Date: Wed, 2 Mar 2022 15:06:03 +0000 Subject: [PATCH 5/8] chore(utils/password_manager): remove type errors --- pyproject.toml | 1 - src/poetry/utils/password_manager.py | 15 ++++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 8c0a8c593fa..9850e682732 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -133,7 +133,6 @@ module = [ 'poetry.repositories.installed_repository', 'poetry.utils.authenticator', 'poetry.utils.env', - 'poetry.utils.password_manager', 'poetry.utils.setup_reader', ] ignore_errors = true diff --git a/src/poetry/utils/password_manager.py b/src/poetry/utils/password_manager.py index fcef51d157e..ec46634e37d 100644 --- a/src/poetry/utils/password_manager.py +++ b/src/poetry/utils/password_manager.py @@ -121,10 +121,10 @@ def _check(self) -> None: class PasswordManager: def __init__(self, config: Config) -> None: self._config = config - self._keyring = None + self._keyring: KeyRing | None = None @property - def keyring(self) -> KeyRing | None: + def keyring(self) -> KeyRing: if self._keyring is None: self._keyring = KeyRing("poetry-repository") if not self._keyring.is_available(): @@ -141,14 +141,15 @@ def set_pypi_token(self, name: str, token: str) -> None: self.keyring.set_password(name, "__token__", token) def get_pypi_token(self, name: str) -> str: - if not self.keyring.is_available(): - return self._config.get(f"pypi-token.{name}") - - return self.keyring.get_password(name, "__token__") + token = self.keyring.get_password(name, "__token__") + if token is not None: + return token + return self._config.get(f"pypi-token.{name}") def delete_pypi_token(self, name: str) -> None: if not self.keyring.is_available(): - return self._config.auth_config_source.remove_property(f"pypi-token.{name}") + self._config.auth_config_source.remove_property(f"pypi-token.{name}") + return self.keyring.delete_password(name, "__token__") From b49de3353fbef9fd7ebfc18ecaa7924145da0298 Mon Sep 17 00:00:00 2001 From: Edwin Yllanes Date: Wed, 2 Mar 2022 16:46:21 +0000 Subject: [PATCH 6/8] chore(utils/authenticator): remove type errors --- pyproject.toml | 1 - src/poetry/utils/authenticator.py | 23 ++++++++++++++--------- src/poetry/utils/exporter.py | 6 +----- src/poetry/utils/password_manager.py | 6 ++++-- src/poetry/utils/types.py | 14 ++++++++++++++ 5 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 src/poetry/utils/types.py diff --git a/pyproject.toml b/pyproject.toml index 9850e682732..05f2397ac58 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -131,7 +131,6 @@ module = [ 'poetry.mixology.version_solver', 'poetry.packages.locker', 'poetry.repositories.installed_repository', - 'poetry.utils.authenticator', 'poetry.utils.env', 'poetry.utils.setup_reader', ] diff --git a/src/poetry/utils/authenticator.py b/src/poetry/utils/authenticator.py index a7d0dda3d89..9ceb53b7f90 100644 --- a/src/poetry/utils/authenticator.py +++ b/src/poetry/utils/authenticator.py @@ -19,6 +19,7 @@ from cleo.io.io import IO from poetry.config.config import Config + from poetry.utils.types import Auth logger = logging.getLogger() @@ -28,8 +29,8 @@ class Authenticator: def __init__(self, config: Config, io: IO | None = None) -> None: self._config = config self._io = io - self._session = None - self._credentials = {} + self._session: requests.Session | None = None + self._credentials: dict[str, tuple[str | None, str | None]] = {} self._password_manager = PasswordManager(self._config) def _log(self, message: str, level: str = "debug") -> None: @@ -69,7 +70,7 @@ def request(self, method: str, url: str, **kwargs: Any) -> requests.Response: ) # Send the request. - send_kwargs = { + send_kwargs: Any = { "timeout": kwargs.get("timeout"), "allow_redirects": kwargs.get("allow_redirects", True), } @@ -117,11 +118,14 @@ def get_credentials_for_url(self, url: str) -> tuple[str | None, str | None]: # Split from the left because that's how urllib.parse.urlsplit() # behaves if more than one : is present (which again can be checked # using the password attribute of the return value) - credentials = auth.split(":", 1) if ":" in auth else (auth, None) - credentials = tuple( - None if x is None else urllib.parse.unquote(x) for x in credentials + credentials_tmp = auth.split(":", 1) if ":" in auth else (auth, None) + credentials_tmp = tuple( + None if x is None else urllib.parse.unquote(x) + for x in credentials_tmp ) + credentials = credentials_tmp[0], credentials_tmp[1] + if credentials[0] is not None or credentials[1] is not None: credentials = (credentials[0] or "", credentials[1] or "") @@ -132,10 +136,10 @@ def get_credentials_for_url(self, url: str) -> tuple[str | None, str | None]: def get_pypi_token(self, name: str) -> str: return self._password_manager.get_pypi_token(name) - def get_http_auth(self, name: str) -> dict[str, str] | None: + def get_http_auth(self, name: str) -> Auth | None: return self._get_http_auth(name, None) - def _get_http_auth(self, name: str, netloc: str | None) -> dict[str, str] | None: + def _get_http_auth(self, name: str, netloc: str | None) -> Auth | None: if name == "pypi": url = "https://upload.pypi.org/legacy/" else: @@ -155,6 +159,7 @@ def _get_http_auth(self, name: str, netloc: str | None) -> dict[str, str] | None ) return auth + return None def _get_credentials_for_netloc(self, netloc: str) -> tuple[str | None, str | None]: for repository_name in self._config.get("repositories", []): @@ -169,7 +174,7 @@ def _get_credentials_for_netloc(self, netloc: str) -> tuple[str | None, str | No def _get_credentials_for_netloc_from_keyring( self, url: str, netloc: str, username: str | None - ) -> dict[str, str] | None: + ) -> Auth | None: import keyring cred = keyring.get_credential(url, username) diff --git a/src/poetry/utils/exporter.py b/src/poetry/utils/exporter.py index 209c7d9a9d4..6bced05e129 100644 --- a/src/poetry/utils/exporter.py +++ b/src/poetry/utils/exporter.py @@ -14,13 +14,9 @@ if TYPE_CHECKING: from pathlib import Path - from typing import Protocol from poetry.poetry import Poetry - - class Writer(Protocol): - def write(self, text: str) -> None: - pass + from poetry.utils.types import Writer class Exporter: diff --git a/src/poetry/utils/password_manager.py b/src/poetry/utils/password_manager.py index ec46634e37d..6eb652bfbcb 100644 --- a/src/poetry/utils/password_manager.py +++ b/src/poetry/utils/password_manager.py @@ -8,6 +8,8 @@ if TYPE_CHECKING: from poetry.config.config import Config + from poetry.utils.types import Auth + logger = logging.getLogger(__name__) @@ -153,7 +155,7 @@ def delete_pypi_token(self, name: str) -> None: self.keyring.delete_password(name, "__token__") - def get_http_auth(self, name: str) -> dict[str, str] | None: + def get_http_auth(self, name: str) -> Auth | None: auth = self._config.get(f"http-basic.{name}") if not auth: username = self._config.get(f"http-basic.{name}.username") @@ -182,7 +184,7 @@ def set_http_password(self, name: str, username: str, password: str) -> None: def delete_http_password(self, name: str) -> None: auth = self.get_http_auth(name) - if not auth or "username" not in auth: + if auth is None or "username" not in auth: return with suppress(KeyRingError): diff --git a/src/poetry/utils/types.py b/src/poetry/utils/types.py new file mode 100644 index 00000000000..be38071fd42 --- /dev/null +++ b/src/poetry/utils/types.py @@ -0,0 +1,14 @@ +from __future__ import annotations + +from typing import Protocol +from typing import TypedDict + + +class Auth(TypedDict): + username: str + password: str | None + + +class Writer(Protocol): + def write(self, text: str) -> None: + pass From e223840e11d64cc3bed38b59d5c2f0c6f4603d3d Mon Sep 17 00:00:00 2001 From: Edwin Yllanes Date: Wed, 2 Mar 2022 18:24:12 +0000 Subject: [PATCH 7/8] chore(utils/setup_reader): some fixes --- .pre-commit-config.yaml | 1 + src/poetry/utils/setup_reader.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0152ce96069..2faf5343e0c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -87,6 +87,7 @@ repos: pass_filenames: false additional_dependencies: - types-requests + - typed-ast - repo: https://github.com/pre-commit/pre-commit rev: v2.17.0 diff --git a/src/poetry/utils/setup_reader.py b/src/poetry/utils/setup_reader.py index baac42c12ff..dbb9fd104a4 100644 --- a/src/poetry/utils/setup_reader.py +++ b/src/poetry/utils/setup_reader.py @@ -312,6 +312,7 @@ def _find_single_string( if variable is not None and isinstance(variable, ast.Str): return variable.s + return None def _find_in_call(self, call: ast.Call, name: str) -> Any | None: for keyword in call.keywords: @@ -342,8 +343,9 @@ def _find_variable_in_body(self, body: Iterable[Any], name: str) -> Any | None: if target.id == name: return elem.value + return None - def _find_in_dict(self, dict_: ast.Dict | ast.Call, name: str) -> Any | None: + def _find_in_dict(self, dict_: ast.Dict, name: str) -> Any | None: for key, val in zip(dict_.keys, dict_.values): if isinstance(key, ast.Str) and key.s == name: return val From 8b53fe2703c89aa3855b3d82ff088a233c340b9f Mon Sep 17 00:00:00 2001 From: Edwin Yllanes Date: Wed, 2 Mar 2022 18:28:12 +0000 Subject: [PATCH 8/8] chore(utils/setup_reader): some fixes --- .pre-commit-config.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2faf5343e0c..0152ce96069 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -87,7 +87,6 @@ repos: pass_filenames: false additional_dependencies: - types-requests - - typed-ast - repo: https://github.com/pre-commit/pre-commit rev: v2.17.0