diff --git a/.flake8 b/.flake8 index d4c3a401341..4a6c7199262 100644 --- a/.flake8 +++ b/.flake8 @@ -2,6 +2,8 @@ min_python_version = 3.6.0 max-line-length = 88 ban-relative-imports = true +# flake8-use-fstring: https://github.com/MichaelKim0407/flake8-use-fstring#--percent-greedy-and---format-greedy +format-greedy = 1 inline-quotes = double # Allow omission of a return type hint for __init__ if at least one argument is annotated # used by flake8-annotations diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index dbe789ebdf8..f176df8c131 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -42,6 +42,7 @@ repos: - flake8-tidy-imports==4.5.0 - flake8-type-checking==1.1.0 - flake8-typing-imports==1.11.0 + - flake8-use-fstring==1.3 - pep8-naming==0.12.1 - repo: https://github.com/asottile/pyupgrade diff --git a/src/poetry/config/config.py b/src/poetry/config/config.py index 720e479f48f..636fa2097dd 100644 --- a/src/poetry/config/config.py +++ b/src/poetry/config/config.py @@ -111,9 +111,7 @@ def get(self, setting_name: str, default: Any = None) -> Any: # Looking in the environment if the setting # is set via a POETRY_* environment variable if self._use_environment: - env = "POETRY_{}".format( - "_".join(k.upper().replace("-", "_") for k in keys) - ) + env = "POETRY_" + "_".join(k.upper().replace("-", "_") for k in keys) env_value = os.getenv(env) if env_value is not None: return self.process(self._get_normalizer(setting_name)(env_value)) diff --git a/src/poetry/console/application.py b/src/poetry/console/application.py index 179f1bdbbd4..158b7bed604 100644 --- a/src/poetry/console/application.py +++ b/src/poetry/console/application.py @@ -39,13 +39,9 @@ def load_command(name: str) -> Callable: def _load() -> Type[Command]: - module = import_module( - "poetry.console.commands.{}".format(".".join(name.split(" "))) - ) - command_class = getattr( - module, "{}Command".format("".join(c.title() for c in name.split(" "))) - ) - + words = name.split(" ") + module = import_module("poetry.console.commands." + ".".join(words)) + command_class = getattr(module, "".join(c.title() for c in words) + "Command") return command_class() return _load diff --git a/src/poetry/console/commands/build.py b/src/poetry/console/commands/build.py index 139e65f89e1..ef54974f89f 100644 --- a/src/poetry/console/commands/build.py +++ b/src/poetry/console/commands/build.py @@ -27,9 +27,7 @@ def handle(self) -> None: package = self.poetry.package self.line( - "Building {} ({})".format( - package.pretty_name, package.version - ) + f"Building {package.pretty_name} ({package.version})" ) builder = Builder(self.poetry) diff --git a/src/poetry/console/commands/cache/clear.py b/src/poetry/console/commands/cache/clear.py index f08092dacdf..8a16f06b6a8 100644 --- a/src/poetry/console/commands/cache/clear.py +++ b/src/poetry/console/commands/cache/clear.py @@ -43,7 +43,7 @@ def handle(self) -> int: if not self.option("all"): raise RuntimeError( "Add the --all option if you want to clear all " - "{} caches".format(parts[0]) + f"{parts[0]} caches" ) if not os.path.exists(str(cache_dir)): diff --git a/src/poetry/console/commands/config.py b/src/poetry/console/commands/config.py index eba22e636b2..96df3cd4514 100644 --- a/src/poetry/console/commands/config.py +++ b/src/poetry/console/commands/config.py @@ -230,7 +230,7 @@ def handle(self) -> Optional[int]: elif len(values) != 2: raise ValueError( "Expected one or two arguments " - "(username, password), got {}".format(len(values)) + f"(username, password), got {len(values)}" ) else: username = values[0] @@ -270,7 +270,7 @@ def handle(self) -> Optional[int]: return 0 - raise ValueError("Setting {} does not exist".format(self.argument("key"))) + raise ValueError(f"Setting {self.argument('key')} does not exist") def _handle_single_value( self, @@ -307,19 +307,16 @@ def _list_configuration(self, config: Dict, raw: Dict, k: str = "") -> None: continue elif isinstance(value, list): - value = [ + value = ", ".join( json.dumps(val) if isinstance(val, list) else val for val in value - ] - - value = "[{}]".format(", ".join(value)) + ) + value = f"[{value}]" if k.startswith("repositories."): - message = "{} = {}".format( - k + key, json.dumps(raw_val) - ) + message = f"{k + key} = {json.dumps(raw_val)}" elif isinstance(raw_val, str) and raw_val != value: - message = "{} = {} # {}".format( - k + key, json.dumps(raw_val), value + message = ( + f"{k + key} = {json.dumps(raw_val)} # {value}" ) else: message = f"{k + key} = {json.dumps(value)}" @@ -361,12 +358,11 @@ def _get_setting( continue if isinstance(value, list): - value = [ + value = ", ".join( json.dumps(val) if isinstance(val, list) else val for val in value - ] - - value = "[{}]".format(", ".join(value)) + ) + value = f"[{value}]" value = json.dumps(value) diff --git a/src/poetry/console/commands/debug/info.py b/src/poetry/console/commands/debug/info.py index 2e02bb5a0b2..cbfbf1402db 100644 --- a/src/poetry/console/commands/debug/info.py +++ b/src/poetry/console/commands/debug/info.py @@ -17,9 +17,7 @@ def handle(self) -> int: "\n".join( [ f"Version: {self.poetry.VERSION}", - "Python: {}".format( - poetry_python_version - ), + f"Python: {poetry_python_version}", ] ) ) diff --git a/src/poetry/console/commands/env/info.py b/src/poetry/console/commands/env/info.py index 93f4d44d529..b3eaf2eb4ac 100644 --- a/src/poetry/console/commands/env/info.py +++ b/src/poetry/console/commands/env/info.py @@ -52,15 +52,14 @@ def _display_complete_info(self, env: "Env") -> None: self.line("") system_env = env.parent_env + python = ".".join(str(v) for v in system_env.version_info[:3]) self.line("System") self.line( "\n".join( [ f"Platform: {env.platform}", f"OS: {env.os}", - "Python: {}".format( - ".".join(str(v) for v in system_env.version_info[:3]) - ), + f"Python: {python}", f"Path: {system_env.path}", f"Executable: {system_env.python}", ] diff --git a/src/poetry/console/commands/init.py b/src/poetry/console/commands/init.py index 815decf76bf..31e33577357 100644 --- a/src/poetry/console/commands/init.py +++ b/src/poetry/console/commands/init.py @@ -146,13 +146,11 @@ def handle(self) -> int: python = self.option("python") if not python: current_env = SystemEnv(Path(sys.executable)) - default_python = "^{}".format( - ".".join(str(v) for v in current_env.version_info[:2]) + default_python = "^" + ".".join( + str(v) for v in current_env.version_info[:2] ) question = self.create_question( - "Compatible Python versions [{}]: ".format( - default_python - ), + f"Compatible Python versions [{default_python}]: ", default=default_python, ) python = self.ask(question) @@ -282,9 +280,7 @@ def _determine_requirements( choices.append(found_package.pretty_name) self.line( - "Found {} packages matching {}".format( - len(matches), package - ) + f"Found {len(matches)} packages matching {package}" ) package = self.choice( @@ -314,9 +310,7 @@ def _determine_requirements( ) self.line( - "Using version {} for {}".format( - package_constraint, package - ) + f"Using version {package_constraint} for {package}" ) constraint["version"] = package_constraint diff --git a/src/poetry/console/commands/install.py b/src/poetry/console/commands/install.py index f90e6d6673d..23274665138 100644 --- a/src/poetry/console/commands/install.py +++ b/src/poetry/console/commands/install.py @@ -176,19 +176,15 @@ def handle(self) -> int: # If this is a true error it will be picked up later by build anyway. return 0 + log_install = ( + f"Installing the current project: {self.poetry.package.pretty_name} " + f"(<{{tag}}>{self.poetry.package.pretty_version})" + ) + overwrite = self._io.output.is_decorated() and not self.io.is_debug() self.line("") - if not self._io.output.is_decorated() or self.io.is_debug(): - self.line( - "Installing the current project: {} ({})".format( - self.poetry.package.pretty_name, self.poetry.package.pretty_version - ) - ) - else: - self.write( - "Installing the current project: {} ({})".format( - self.poetry.package.pretty_name, self.poetry.package.pretty_version - ) - ) + self.write(log_install.format(tag="c2")) + if not overwrite: + self.line("") if self.option("dry-run"): self.line("") @@ -196,12 +192,8 @@ def handle(self) -> int: builder.build() - if self._io.output.is_decorated() and not self.io.is_debug(): - self.overwrite( - "Installing the current project: {} ({})".format( - self.poetry.package.pretty_name, self.poetry.package.pretty_version - ) - ) + if overwrite: + self.overwrite(log_install.format(tag="success")) self.line("") return 0 diff --git a/src/poetry/console/commands/new.py b/src/poetry/console/commands/new.py index e157f555b2c..af4c0c5fdf6 100644 --- a/src/poetry/console/commands/new.py +++ b/src/poetry/console/commands/new.py @@ -50,7 +50,7 @@ def handle(self) -> None: if path.exists() and list(path.glob("*")): # Directory is not empty. Aborting. raise RuntimeError( - "Destination {} " "exists and is not empty".format(path) + f"Destination {path} " "exists and is not empty" ) readme_format = self.option("readme") or "md" @@ -64,9 +64,7 @@ def handle(self) -> None: author += f" <{author_email}>" current_env = SystemEnv(Path(sys.executable)) - default_python = "^{}".format( - ".".join(str(v) for v in current_env.version_info[:2]) - ) + default_python = "^" + ".".join(str(v) for v in current_env.version_info[:2]) layout_ = layout_( name, @@ -83,7 +81,5 @@ def handle(self) -> None: path = path.relative_to(Path.cwd()) self.line( - "Created package {} in {}".format( - layout_._package_name, path.as_posix() - ) + f"Created package {layout_._package_name} in {path.as_posix()}" ) diff --git a/src/poetry/console/commands/plugin/show.py b/src/poetry/console/commands/plugin/show.py index 8ca6290d870..bd3d66d1856 100644 --- a/src/poetry/console/commands/plugin/show.py +++ b/src/poetry/console/commands/plugin/show.py @@ -24,6 +24,7 @@ def handle(self) -> int: from poetry.repositories.installed_repository import InstalledRepository from poetry.utils.env import EnvManager from poetry.utils.helpers import canonicalize_name + from poetry.utils.helpers import pluralize plugins: DefaultDict[str, Dict[str, Union["Package", List[str]]]] = defaultdict( lambda: { @@ -57,27 +58,21 @@ def handle(self) -> int: for name, info in plugins.items(): package = info["package"] + description = " " + package.description if package.description else "" self.line("") - self.line( - " • {} ({}){}".format( - name, - package.version, - " " + package.description if package.description else "", - ) - ) + self.line(f" • {name} ({package.version}){description}") provide_line = " " if info["plugins"]: - provide_line += " {} plugin{}".format( - len(info["plugins"]), "s" if len(info["plugins"]) > 1 else "" - ) + count = len(info["plugins"]) + provide_line += f" {count} plugin{pluralize(count)}" if info["application_plugins"]: if info["plugins"]: provide_line += " and" - provide_line += " {} application plugin{}".format( - len(info["application_plugins"]), - "s" if len(info["application_plugins"]) > 1 else "", + count = len(info["application_plugins"]) + provide_line += ( + f" {count} application plugin{pluralize(count)}" ) self.line(provide_line) @@ -87,9 +82,7 @@ def handle(self) -> int: self.line(" Dependencies") for dependency in package.requires: self.line( - " - {} ({})".format( - dependency.pretty_name, dependency.pretty_constraint - ) + f" - {dependency.pretty_name} ({dependency.pretty_constraint})" ) return 0 diff --git a/src/poetry/console/commands/publish.py b/src/poetry/console/commands/publish.py index 1cbd9e04868..f2720621bce 100644 --- a/src/poetry/console/commands/publish.py +++ b/src/poetry/console/commands/publish.py @@ -49,8 +49,8 @@ def handle(self) -> Optional[int]: # Building package first, if told if self.option("build"): if publisher.files and not self.confirm( - "There are {} files ready for publishing. " - "Build anyway?".format(len(publisher.files)) + f"There are {len(publisher.files)} files ready for publishing. " + "Build anyway?" ): self.line_error("Aborted!") diff --git a/src/poetry/console/commands/remove.py b/src/poetry/console/commands/remove.py index 0e02100d1db..143567c51d6 100644 --- a/src/poetry/console/commands/remove.py +++ b/src/poetry/console/commands/remove.py @@ -88,9 +88,7 @@ def handle(self) -> int: not_found = set(packages).difference(removed) if not_found: raise ValueError( - "The following packages were not found: {}".format( - ", ".join(sorted(not_found)) - ) + "The following packages were not found: " + ", ".join(sorted(not_found)) ) # Refresh the locker diff --git a/src/poetry/console/commands/run.py b/src/poetry/console/commands/run.py index 74be75191f3..cba38a1e584 100644 --- a/src/poetry/console/commands/run.py +++ b/src/poetry/console/commands/run.py @@ -54,8 +54,8 @@ def run_script(self, script: Union[str, dict], args: str) -> Any: cmd += [ "import sys; " "from importlib import import_module; " - "sys.argv = {!r}; {}" - "import_module('{}').{}()".format(args, src_in_sys_path, module, callable_) + f"sys.argv = {args!r}; {src_in_sys_path}" + f"import_module('{module}').{callable_}()" ] return self.env.execute(*cmd) diff --git a/src/poetry/console/commands/self/update.py b/src/poetry/console/commands/self/update.py index bb17ceda1dd..eefa6397646 100644 --- a/src/poetry/console/commands/self/update.py +++ b/src/poetry/console/commands/self/update.py @@ -139,9 +139,7 @@ def handle(self) -> int: self.line("") self.line( - "Poetry ({}) is installed now. Great!".format( - release.version - ) + f"Poetry ({release.version}) is installed now. Great!" ) return 0 diff --git a/src/poetry/console/commands/shell.py b/src/poetry/console/commands/shell.py index b75b9eb8b8f..3572d361964 100644 --- a/src/poetry/console/commands/shell.py +++ b/src/poetry/console/commands/shell.py @@ -25,8 +25,7 @@ def handle(self) -> None: ) == str(self.env.path) if venv_activated: self.line( - "Virtual environment already activated: " - "{}".format(self.env.path) + f"Virtual environment already activated: {self.env.path}" ) return diff --git a/src/poetry/console/commands/show.py b/src/poetry/console/commands/show.py index 23d076347ee..c192596bb8b 100644 --- a/src/poetry/console/commands/show.py +++ b/src/poetry/console/commands/show.py @@ -201,9 +201,7 @@ def handle(self) -> Optional[int]: self.line("dependencies") for dependency in pkg.requires: self.line( - " - {} {}".format( - dependency.pretty_name, dependency.pretty_constraint - ) + f" - {dependency.pretty_name} {dependency.pretty_constraint}" ) if required_by: @@ -303,16 +301,12 @@ def handle(self) -> Optional[int]: ): continue - line = "{:{}}{}".format( - color, name, name_length - len(install_marker), install_marker - ) + line = f"{name:{name_length - len(install_marker)}}{install_marker}" if write_version: - line += " {:{}}".format( - get_package_version_display_string( - locked, root=self.poetry.file.parent - ), - version_length, + version = get_package_version_display_string( + locked, root=self.poetry.file.parent ) + line += f" {version:{version_length}}" if show_latest: latest = latest_packages[locked.pretty_name] update_status = latest_statuses[locked.pretty_name] @@ -324,13 +318,10 @@ def handle(self) -> Optional[int]: elif update_status == "update-possible": color = "yellow" - line += " {:{}}".format( - color, - get_package_version_display_string( - latest, root=self.poetry.file.parent - ), - latest_length, + version = get_package_version_display_string( + latest, root=self.poetry.file.parent ) + line += f" {version:{latest_length}}" if write_description: description = locked.description @@ -366,12 +357,7 @@ def display_package_tree( level = 1 color = self.colors[level] - info = "{tree_bar}── <{color}>{name} {constraint}".format( - tree_bar=tree_bar, - color=color, - name=dependency.name, - constraint=dependency.pretty_constraint, - ) + info = f"{tree_bar}── <{color}>{dependency.name} {dependency.pretty_constraint}" self._write_tree_line(io, info) tree_bar = tree_bar.replace("└", " ") @@ -414,13 +400,7 @@ def _display_tree( if dependency.name in current_tree: circular_warn = "(circular dependency aborted here)" - info = "{tree_bar}── <{color}>{name} {constraint} {warn}".format( - tree_bar=tree_bar, - color=color, - name=dependency.name, - constraint=dependency.pretty_constraint, - warn=circular_warn, - ) + info = f"{tree_bar}── <{color}>{dependency.name} {dependency.pretty_constraint} {circular_warn}" self._write_tree_line(io, info) tree_bar = tree_bar.replace("└", " ") diff --git a/src/poetry/console/commands/version.py b/src/poetry/console/commands/version.py index ff3148ff87d..54875a746e2 100644 --- a/src/poetry/console/commands/version.py +++ b/src/poetry/console/commands/version.py @@ -58,9 +58,7 @@ def handle(self) -> None: self.line(version.to_string()) else: self.line( - "Bumping version from {} to {}".format( - self.poetry.package.pretty_version, version - ) + f"Bumping version from {self.poetry.package.pretty_version} to {version}" ) content = self.poetry.file.read() diff --git a/src/poetry/factory.py b/src/poetry/factory.py index 113dd0f536b..b461bccf9ad 100644 --- a/src/poetry/factory.py +++ b/src/poetry/factory.py @@ -100,9 +100,7 @@ def create_config(cls, io: Optional["IO"] = None) -> Config: if config_file.exists(): if io.is_debug(): io.write_line( - "Loading configuration file {}".format( - config_file.path - ) + f"Loading configuration file {config_file.path}" ) config.merge(config_file.read()) @@ -114,9 +112,7 @@ def create_config(cls, io: Optional["IO"] = None) -> Config: if auth_config_file.exists(): if io.is_debug(): io.write_line( - "Loading configuration file {}".format( - auth_config_file.path - ) + f"Loading configuration file {auth_config_file.path}" ) config.merge(auth_config_file.read()) @@ -134,9 +130,7 @@ def configure_sources( is_default = source.get("default", False) is_secondary = source.get("secondary", False) if io.is_debug(): - message = "Adding repository {} ({})".format( - repository.name, repository.url - ) + message = f"Adding repository {repository.name} ({repository.url})" if is_default: message += " and setting it as the default one" elif is_secondary: diff --git a/src/poetry/inspection/info.py b/src/poetry/inspection/info.py index 9b631163dd9..992920f107d 100644 --- a/src/poetry/inspection/info.py +++ b/src/poetry/inspection/info.py @@ -49,7 +49,7 @@ class PackageInfoError(ValueError): def __init__( self, path: Union[Path, str], *reasons: Union[BaseException, str] ) -> None: - reasons = (f"Unable to determine package info for path: {str(path)}",) + reasons + reasons = (f"Unable to determine package info for path: {path!s}",) + reasons super().__init__("\n\n".join(str(msg).strip() for msg in reasons if msg)) @@ -178,8 +178,8 @@ def to_package( except ValueError: # Likely unable to parse constraint so we skip it self._log( - "Invalid constraint ({}) found in {}-{} dependencies, " - "skipping".format(req, package.name, package.version), + f"Invalid constraint ({req}) found in {package.name}-{package.version} dependencies, " + "skipping", level="warning", ) continue @@ -460,6 +460,10 @@ def _pep517_metadata(cls, path: Path) -> "PackageInfo": dest_dir = venv.path.parent / "dist" dest_dir.mkdir() + pep517_meta_build_script = PEP517_META_BUILD.format( + source=path.as_posix(), dest=dest_dir.as_posix() + ) + try: venv.run_pip( "install", @@ -470,9 +474,7 @@ def _pep517_metadata(cls, path: Path) -> "PackageInfo": venv.run( "python", "-", - input_=PEP517_META_BUILD.format( - source=path.as_posix(), dest=dest_dir.as_posix() - ), + input_=pep517_meta_build_script, ) return cls.from_metadata(dest_dir) except EnvCommandError as e: diff --git a/src/poetry/installation/chooser.py b/src/poetry/installation/chooser.py index 8bd9de7d86f..789590a2324 100644 --- a/src/poetry/installation/chooser.py +++ b/src/poetry/installation/chooser.py @@ -149,8 +149,8 @@ def _sort_key(self, package: "Package", link: "Link") -> Tuple: wheel = Wheel(link.filename) if not wheel.is_supported_by_environment(self._env): raise RuntimeError( - "{} is not a supported wheel for this platform. It " - "can't be sorted.".format(wheel.filename) + f"{wheel.filename} is not a supported wheel for this platform. It " + "can't be sorted." ) # TODO: Binary preference diff --git a/src/poetry/installation/executor.py b/src/poetry/installation/executor.py index 8f25d2ef51b..3b8c920c2bf 100644 --- a/src/poetry/installation/executor.py +++ b/src/poetry/installation/executor.py @@ -26,6 +26,7 @@ from poetry.utils._compat import decode from poetry.utils.authenticator import Authenticator from poetry.utils.env import EnvCommandError +from poetry.utils.helpers import pluralize from poetry.utils.helpers import safe_rmtree from poetry.utils.pip import pip_editable_install from poetry.utils.pip import pip_install @@ -221,6 +222,7 @@ def _write(self, operation: "OperationTypes", line: str) -> None: def _execute_operation(self, operation: "OperationTypes") -> None: try: + op_message = self.get_operation_message(operation) if self.supports_fancy_output(): if id(operation) not in self._sections and self._should_write_operation( operation @@ -228,27 +230,20 @@ def _execute_operation(self, operation: "OperationTypes") -> None: with self._lock: self._sections[id(operation)] = self._io.section() self._sections[id(operation)].write_line( - " • {message}: Pending...".format( - message=self.get_operation_message(operation), - ), + f" • {op_message}: Pending..." ) else: if self._should_write_operation(operation): if not operation.skipped: self._io.write_line( - " • {message}".format( - message=self.get_operation_message(operation), - ), + f" • {op_message}" ) else: self._io.write_line( - " • {message}: " + f" • {op_message}: " "Skipped " "for the following reason: " - "{reason}".format( - message=self.get_operation_message(operation), - reason=operation.skip_reason, - ) + f"{operation.skip_reason}" ) try: @@ -271,11 +266,7 @@ def _execute_operation(self, operation: "OperationTypes") -> None: if not self.supports_fancy_output(): io = self._io else: - message = ( - " • {message}: Failed".format( - message=self.get_operation_message(operation, error=True), - ) - ) + message = f" • {self.get_operation_message(operation, error=True)}: Failed" self._write(operation, message) io = self._sections.get(id(operation), self._io) @@ -288,9 +279,7 @@ def _execute_operation(self, operation: "OperationTypes") -> None: self._shutdown = True except KeyboardInterrupt: try: - message = " • {message}: Cancelled".format( - message=self.get_operation_message(operation, warning=True), - ) + message = f" • {self.get_operation_message(operation, warning=True)}: Cancelled" if not self.supports_fancy_output(): self._io.write_line(message) else: @@ -307,13 +296,10 @@ def _do_execute_operation(self, operation: "OperationTypes") -> int: if self.supports_fancy_output(): self._write( operation, - " • {message}: " + f" • {operation_message}: " "Skipped " "for the following reason: " - "{reason}".format( - message=operation_message, - reason=operation.skip_reason, - ), + f"{operation.skip_reason}", ) self._skipped[operation.job_type] += 1 @@ -321,11 +307,7 @@ def _do_execute_operation(self, operation: "OperationTypes") -> int: return 0 if not self._enabled or self._dry_run: - self._io.write_line( - " • {message}".format( - message=operation_message, - ) - ) + self._io.write_line(f" • {operation_message}") return 0 @@ -334,9 +316,8 @@ def _do_execute_operation(self, operation: "OperationTypes") -> int: if result != 0: return result - message = " • {message}".format( - message=self.get_operation_message(operation, done=True), - ) + operation_message = self.get_operation_message(operation, done=True) + message = f" • {operation_message}" self._write(operation, message) self._increment_operations_count(operation, True) @@ -394,38 +375,23 @@ def get_operation_message( package_color += "_dark" if operation.job_type == "install": - return "<{}>Installing <{}>{} (<{}>{})".format( - base_tag, - package_color, - operation.package.name, - package_color, - operation_color, - operation.package.full_pretty_version, + return ( + f"<{base_tag}>Installing <{package_color}>{operation.package.name} " + f"(<{operation_color}>{operation.package.full_pretty_version})" ) if operation.job_type == "uninstall": - return "<{}>Removing <{}>{} (<{}>{})".format( - base_tag, - package_color, - operation.package.name, - package_color, - operation_color, - operation.package.full_pretty_version, + return ( + f"<{base_tag}>Removing <{package_color}>{operation.package.name} " + f"(<{operation_color}>{operation.package.full_pretty_version})" ) if operation.job_type == "update": - return "<{}>Updating <{}>{} (<{}>{} -> <{}>{})".format( - base_tag, - package_color, - operation.initial_package.name, - package_color, - source_operation_color, - operation.initial_package.full_pretty_version, - source_operation_color, - operation_color, - operation.target_package.full_pretty_version, + return ( + f"<{base_tag}>Updating <{package_color}>{operation.initial_package.name} " + f"(<{source_operation_color}>{operation.initial_package.full_pretty_version} " + f"-> <{operation_color}>{operation.target_package.full_pretty_version})" ) - return "" def _display_summary(self, operations: List["OperationTypes"]) -> None: @@ -452,21 +418,13 @@ def _display_summary(self, operations: List["OperationTypes"]) -> None: return self._io.write_line("") - self._io.write_line( - "Package operations: " - "{} install{}, " - "{} update{}, " - "{} removal{}" - "{}".format( - installs, - "" if installs == 1 else "s", - updates, - "" if updates == 1 else "s", - uninstalls, - "" if uninstalls == 1 else "s", - f", {skipped} skipped" if skipped and self._verbose else "", - ) - ) + self._io.write("Package operations: ") + self._io.write(f"{installs} install{pluralize(installs)}, ") + self._io.write(f"{updates} update{pluralize(updates)}, ") + self._io.write(f"{uninstalls} removal{pluralize(uninstalls)}") + if skipped and self._verbose: + self._io.write(f", {skipped} skipped") + self._io.write_line("") self._io.write_line("") def _execute_install(self, operation: Union["Install", "Update"]) -> int: @@ -484,11 +442,8 @@ def _execute_update(self, operation: Union["Install", "Update"]) -> int: return status_code def _execute_uninstall(self, operation: "Uninstall") -> int: - message = ( - " • {message}: Removing...".format( - message=self.get_operation_message(operation), - ) - ) + op_msg = self.get_operation_message(operation) + message = f" • {op_msg}: Removing..." self._write(operation, message) return self._remove(operation) @@ -509,11 +464,7 @@ def _install(self, operation: Union["Install", "Update"]) -> int: archive = self._download(operation) operation_message = self.get_operation_message(operation) - message = ( - " • {message}: Installing...".format( - message=operation_message, - ) - ) + message = f" • {operation_message}: Installing..." self._write(operation, message) return self.pip_install(archive, upgrade=operation.job_type == "update") @@ -539,12 +490,9 @@ def _remove(self, operation: "Uninstall") -> int: def _prepare_file(self, operation: Union["Install", "Update"]) -> Path: package = operation.package + operation_message = self.get_operation_message(operation) - message = ( - " • {message}: Preparing...".format( - message=self.get_operation_message(operation), - ) - ) + message = f" • {operation_message}: Preparing..." self._write(operation, message) archive = Path(package.source_url) @@ -561,11 +509,7 @@ def _install_directory(self, operation: Union["Install", "Update"]) -> int: package = operation.package operation_message = self.get_operation_message(operation) - message = ( - " • {message}: Building...".format( - message=operation_message, - ) - ) + message = f" • {operation_message}: Building..." self._write(operation, message) if package.root_dir: @@ -622,9 +566,7 @@ def _install_git(self, operation: Union["Install", "Update"]) -> int: operation_message = self.get_operation_message(operation) message = ( - " • {message}: Cloning...".format( - message=operation_message, - ) + f" • {operation_message}: Cloning..." ) self._write(operation, message) @@ -716,9 +658,7 @@ def _download_archive( wheel_size = response.headers.get("content-length") operation_message = self.get_operation_message(operation) message = ( - " • {message}: Downloading...".format( - message=operation_message, - ) + f" • {operation_message}: Downloading..." ) progress = None if self.supports_fancy_output(): diff --git a/src/poetry/installation/installer.py b/src/poetry/installation/installer.py index fd4835d7941..97f36e3f30d 100644 --- a/src/poetry/installation/installer.py +++ b/src/poetry/installation/installer.py @@ -17,6 +17,7 @@ from poetry.repositories.installed_repository import InstalledRepository from poetry.utils.extras import get_extra_package_names from poetry.utils.helpers import canonicalize_name +from poetry.utils.helpers import pluralize if TYPE_CHECKING: @@ -385,23 +386,13 @@ def _execute(self, operations: List["OperationTypes"]) -> int: uninstalls += 1 self._io.write_line("") - self._io.write_line( - "Package operations: " - "{} install{}, " - "{} update{}, " - "{} removal{}" - "{}".format( - installs, - "" if installs == 1 else "s", - updates, - "" if updates == 1 else "s", - uninstalls, - "" if uninstalls == 1 else "s", - f", {skipped} skipped" - if skipped and self.is_verbose() - else "", - ) - ) + self._io.write("Package operations: ") + self._io.write(f"{installs} install{pluralize(installs)}, ") + self._io.write(f"{updates} update{pluralize(updates)}, ") + self._io.write(f"{uninstalls} removal{pluralize(uninstalls)}") + if skipped and self.is_verbose(): + self._io.write(f", {skipped} skipped") + self._io.write_line("") self._io.write_line("") @@ -419,23 +410,18 @@ def _execute_operation(self, operation: "Operation") -> None: getattr(self, f"_execute_{method}")(operation) def _execute_install(self, operation: Install) -> None: + target = operation.package if operation.skipped: if self.is_verbose() and (self._execute_operations or self.is_dry_run()): self._io.write_line( - " - Skipping {} ({}) {}".format( - operation.package.pretty_name, - operation.package.full_pretty_version, - operation.skip_reason, - ) + f" - Skipping {target.pretty_name} ({target.full_pretty_version}) {operation.skip_reason}" ) return if self._execute_operations or self.is_dry_run(): self._io.write_line( - " - Installing {} ({})".format( - operation.package.pretty_name, operation.package.full_pretty_version - ) + f" - Installing {target.pretty_name} ({target.full_pretty_version})" ) if not self._execute_operations: @@ -450,22 +436,16 @@ def _execute_update(self, operation: Update) -> None: if operation.skipped: if self.is_verbose() and (self._execute_operations or self.is_dry_run()): self._io.write_line( - " - Skipping {} ({}) {}".format( - target.pretty_name, - target.full_pretty_version, - operation.skip_reason, - ) + f" - Skipping {target.pretty_name} " + f"({target.full_pretty_version}) {operation.skip_reason}" ) return if self._execute_operations or self.is_dry_run(): self._io.write_line( - " - Updating {} ({} -> {})".format( - target.pretty_name, - source.full_pretty_version, - target.full_pretty_version, - ) + f" - Updating {target.pretty_name} " + f"({source.full_pretty_version} -> {target.full_pretty_version})" ) if not self._execute_operations: @@ -474,23 +454,18 @@ def _execute_update(self, operation: Update) -> None: self._installer.update(source, target) def _execute_uninstall(self, operation: Uninstall) -> None: + target = operation.package if operation.skipped: if self.is_verbose() and (self._execute_operations or self.is_dry_run()): self._io.write_line( - " - Not removing {} ({}) {}".format( - operation.package.pretty_name, - operation.package.full_pretty_version, - operation.skip_reason, - ) + f" - Not removing {target.pretty_name} ({target.pretty_version}) {operation.skip_reason}" ) return if self._execute_operations or self.is_dry_run(): self._io.write_line( - " - Removing {} ({})".format( - operation.package.pretty_name, operation.package.full_pretty_version - ) + f" - Removing {target.pretty_name} ({target.pretty_version})" ) if not self._execute_operations: diff --git a/src/poetry/installation/operations/install.py b/src/poetry/installation/operations/install.py index b8b4557f18c..5205d746037 100644 --- a/src/poetry/installation/operations/install.py +++ b/src/poetry/installation/operations/install.py @@ -25,11 +25,7 @@ def job_type(self) -> str: return "install" def __str__(self) -> str: - return "Installing {} ({})".format( - self.package.pretty_name, self.format_version(self.package) - ) + return f"Installing {self.package.pretty_name} ({self.format_version(self.package)})" def __repr__(self) -> str: - return "".format( - self.package.pretty_name, self.format_version(self.package) - ) + return f"" diff --git a/src/poetry/installation/operations/uninstall.py b/src/poetry/installation/operations/uninstall.py index 0d2fb2eed26..c2ea5303145 100644 --- a/src/poetry/installation/operations/uninstall.py +++ b/src/poetry/installation/operations/uninstall.py @@ -28,11 +28,7 @@ def job_type(self) -> str: return "uninstall" def __str__(self) -> str: - return "Uninstalling {} ({})".format( - self.package.pretty_name, self.format_version(self._package) - ) + return f"Uninstalling {self.package.pretty_name} ({self.format_version(self._package)})" def __repr__(self) -> str: - return "".format( - self.package.pretty_name, self.format_version(self.package) - ) + return f"" diff --git a/src/poetry/installation/operations/update.py b/src/poetry/installation/operations/update.py index f9e50f63187..68ff1b66126 100644 --- a/src/poetry/installation/operations/update.py +++ b/src/poetry/installation/operations/update.py @@ -38,17 +38,17 @@ def job_type(self) -> str: return "update" def __str__(self) -> str: - return "Updating {} ({}) to {} ({})".format( - self.initial_package.pretty_name, - self.format_version(self.initial_package), - self.target_package.pretty_name, - self.format_version(self.target_package), + init_version = self.format_version(self.initial_package) + target_version = self.format_version(self.target_package) + return ( + f"Updating {self.initial_package.pretty_name} ({init_version}) " + f"to {self.target_package.pretty_name} ({target_version})" ) def __repr__(self) -> str: - return "".format( - self.initial_package.pretty_name, - self.format_version(self.initial_package), - self.target_package.pretty_name, - self.format_version(self.target_package), + init_version = self.format_version(self.initial_package) + target_version = self.format_version(self.target_package) + return ( + f"" ) diff --git a/src/poetry/installation/pip_installer.py b/src/poetry/installation/pip_installer.py index 95c4c05b0f3..abefac6da16 100644 --- a/src/poetry/installation/pip_installer.py +++ b/src/poetry/installation/pip_installer.py @@ -52,9 +52,7 @@ def install(self, package: "Package", update: bool = False) -> None: parsed = urllib.parse.urlparse(package.source_url) if parsed.scheme == "http": self._io.write_error( - " Installing from unsecure host: {}".format( - parsed.hostname - ) + f" Installing from unsecure host: {parsed.hostname}" ) args += ["--trusted-host", parsed.hostname] @@ -161,9 +159,7 @@ def requirement(self, package: "Package", formatted: bool = False) -> str: return req if package.source_type == "git": - req = "git+{}@{}#egg={}".format( - package.source_url, package.source_reference, package.name - ) + req = f"git+{package.source_url}@{package.source_reference}#egg={package.name}" if package.develop: req = ["-e", req] diff --git a/src/poetry/json/__init__.py b/src/poetry/json/__init__.py index ad4eafed7a9..0515f6b311b 100644 --- a/src/poetry/json/__init__.py +++ b/src/poetry/json/__init__.py @@ -31,9 +31,8 @@ def validate_object(obj: dict, schema_name: str) -> List[str]: for error in validation_errors: message = error.message if error.path: - message = "[{}] {}".format( - ".".join(str(x) for x in error.absolute_path), message - ) + path = ".".join(str(x) for x in error.absolute_path) + message = f"[{path}] {message}" errors.append(message) diff --git a/src/poetry/layouts/layout.py b/src/poetry/layouts/layout.py index 02f155d8c77..abd1e87deb7 100644 --- a/src/poetry/layouts/layout.py +++ b/src/poetry/layouts/layout.py @@ -61,10 +61,9 @@ def __init__( self._readme_format = readme_format.lower() if self._readme_format not in self.ACCEPTED_README_FORMATS: + accepted_readme_formats = ", ".join(self.ACCEPTED_README_FORMATS) raise ValueError( - "Invalid readme format '{}', use one of {}.".format( - readme_format, ", ".join(self.ACCEPTED_README_FORMATS) - ) + f"Invalid readme format '{readme_format}', use one of {accepted_readme_formats}." ) self._license = license diff --git a/src/poetry/masonry/builders/editable.py b/src/poetry/masonry/builders/editable.py index d4c7c71c454..387f9c2394e 100644 --- a/src/poetry/masonry/builders/editable.py +++ b/src/poetry/masonry/builders/editable.py @@ -47,9 +47,7 @@ def __init__(self, poetry: "Poetry", env: "Env", io: "IO") -> None: def build(self) -> None: self._debug( - " - Building package {} in editable mode".format( - self._package.name - ) + f" - Building package {self._package.name} in editable mode" ) if self._package.build_script: @@ -65,9 +63,7 @@ def build(self) -> None: distribution_name=self._package.name ): self._debug( - " - Removed {} directory from {}".format( - removed.name, removed.parent - ) + f" - Removed {removed.name} directory from {removed.parent}" ) added_files = [] @@ -128,9 +124,7 @@ def _add_pth(self) -> List[Path]: # remove any pre-existing pth files for this package for file in self._env.site_packages.find(path=pth_file, writable_only=True): self._debug( - " - Removing existing {} from {} for {}".format( - file.name, file.parent, self._poetry.file.parent - ) + f" - Removing existing {file.name} from {file.parent} for {self._poetry.file.parent}" ) # We can't use unlink(missing_ok=True) because it's not always available if file.exists(): @@ -141,17 +135,13 @@ def _add_pth(self) -> List[Path]: pth_file, content, encoding="utf-8" ) self._debug( - " - Adding {} to {} for {}".format( - pth_file.name, pth_file.parent, self._poetry.file.parent - ) + f" - Adding {pth_file.name} to {pth_file.parent} for {self._poetry.file.parent}" ) return [pth_file] except OSError: # TODO: Replace with PermissionError self._io.write_error_line( - " - Failed to create {} for {}".format( - pth_file.name, self._poetry.file.parent - ) + f" - Failed to create {pth_file.name} for {self._poetry.file.parent}" ) return [] @@ -164,9 +154,7 @@ def _add_scripts(self) -> List[Path]: break else: self._io.write_error_line( - " - Failed to find a suitable script installation directory for {}".format( - self._poetry.file.parent - ) + f" - Failed to find a suitable script installation directory for {self._poetry.file.parent}" ) return [] @@ -178,9 +166,7 @@ def _add_scripts(self) -> List[Path]: script_file = scripts_path.joinpath(name) self._debug( - " - Adding the {} script to {}".format( - name, scripts_path - ) + f" - Adding the {name} script to {scripts_path}" ) with script_file.open("w", encoding="utf-8") as f: f.write( @@ -202,9 +188,7 @@ def _add_scripts(self) -> List[Path]: cmd_script = script_file.with_suffix(".cmd") cmd = WINDOWS_CMD_TEMPLATE.format(python=self._env.python, script=name) self._debug( - " - Adding the {} script wrapper to {}".format( - cmd_script.name, scripts_path - ) + f" - Adding the {cmd_script.name} script wrapper to {scripts_path}" ) with cmd_script.open("w", encoding="utf-8") as f: @@ -223,9 +207,7 @@ def _add_dist_info(self, added_files: List[Path]) -> None: dist_info = self._env.site_packages.mkdir(Path(builder.dist_info)) self._debug( - " - Adding the {} directory to {}".format( - dist_info.name, dist_info.parent - ) + f" - Adding the {dist_info.name} directory to {dist_info.parent}" ) with dist_info.joinpath("METADATA").open("w", encoding="utf-8") as f: @@ -246,14 +228,15 @@ def _add_dist_info(self, added_files: List[Path]) -> None: added_files.append(dist_info.joinpath("entry_points.txt")) - with dist_info.joinpath("RECORD").open("w", encoding="utf-8") as f: + record = dist_info.joinpath("RECORD") + with record.open("w", encoding="utf-8") as f: for path in added_files: hash = self._get_file_hash(path) size = path.stat().st_size - f.write(f"{str(path)},sha256={hash},{size}\n") + f.write(f"{path!s},sha256={hash},{size}\n") # RECORD itself is recorded with no hash or size - f.write("{},,\n".format(dist_info.joinpath("RECORD"))) + f.write(f"{record},,\n") def _get_file_hash(self, filepath: Path) -> str: hashsum = hashlib.sha256() diff --git a/src/poetry/mixology/failure.py b/src/poetry/mixology/failure.py index 64942919802..ad687d74d08 100644 --- a/src/poetry/mixology/failure.py +++ b/src/poetry/mixology/failure.py @@ -43,11 +43,9 @@ def write(self) -> str: if isinstance(incompatibility.cause, PythonCause): if not required_python_version_notification: buffer.append( - "The current project's Python requirement ({}) " + f"The current project's Python requirement ({incompatibility.cause.root_python_version}) " "is not compatible with some of the required " - "packages Python requirement:".format( - incompatibility.cause.root_python_version - ) + "packages Python requirement:" ) required_python_version_notification = True @@ -56,11 +54,9 @@ def write(self) -> str: ) constraint = parse_constraint(incompatibility.cause.python_version) buffer.append( - " - {} requires Python {}, so it will not be satisfied for Python {}".format( - incompatibility.terms[0].dependency.name, - incompatibility.cause.python_version, - root_constraint.difference(constraint), - ) + f" - {incompatibility.terms[0].dependency.name} requires Python " + f"{incompatibility.cause.python_version}, so it will not be satisfied " + f"for Python {root_constraint.difference(constraint)}" ) if required_python_version_notification: @@ -128,14 +124,12 @@ def _visit( other_line = self._line_numbers.get(cause.other) if conflict_line is not None and other_line is not None: + reason = cause.conflict.and_to_string( + cause.other, details_for_cause, conflict_line, other_line + ) self._write( incompatibility, - "Because {}, {}.".format( - cause.conflict.and_to_string( - cause.other, details_for_cause, conflict_line, other_line - ), - incompatibility_string, - ), + f"Because {reason}, {incompatibility_string}.", numbered=numbered, ) elif conflict_line is not None or other_line is not None: @@ -151,9 +145,7 @@ def _visit( self._visit(without_line, details_for_cause) self._write( incompatibility, - "{} because {} ({}), {}.".format( - conjunction, str(with_line), line, incompatibility_string - ), + f"{conjunction} because {with_line!s} ({line}), {incompatibility_string}.", numbered=numbered, ) else: @@ -178,12 +170,7 @@ def _visit( self._write( incompatibility, - "{} because {} ({}), {}".format( - conjunction, - str(cause.conflict), - self._line_numbers[cause.conflict], - incompatibility_string, - ), + f"{conjunction} because {cause.conflict!s} ({self._line_numbers[cause.conflict]}), {incompatibility_string}", numbered=numbered, ) elif isinstance(cause.conflict.cause, ConflictCause) or isinstance( @@ -202,14 +189,12 @@ def _visit( derived_line = self._line_numbers.get(derived) if derived_line is not None: + reason = ext.and_to_string( + derived, details_for_cause, None, derived_line + ) self._write( incompatibility, - "Because {}, {}.".format( - ext.and_to_string( - derived, details_for_cause, None, derived_line - ), - incompatibility_string, - ), + f"Because {reason}, {incompatibility_string}.", numbered=numbered, ) elif self._is_collapsible(derived): @@ -227,33 +212,26 @@ def _visit( details_for_cause = {} self._visit(collapsed_derived, details_for_cause) + reason = collapsed_ext.and_to_string(ext, details_for_cause, None, None) self._write( incompatibility, - "{} because {}, {}.".format( - conjunction, - collapsed_ext.and_to_string(ext, details_for_cause, None, None), - incompatibility_string, - ), + f"{conjunction} because {reason}, {incompatibility_string}.", numbered=numbered, ) else: self._visit(derived, details_for_cause) self._write( incompatibility, - "{} because {}, {}.".format( - conjunction, str(ext), incompatibility_string - ), + f"{conjunction} because {ext!s}, {incompatibility_string}.", numbered=numbered, ) else: + reason = cause.conflict.and_to_string( + cause.other, details_for_cause, None, None + ) self._write( incompatibility, - "Because {}, {}.".format( - cause.conflict.and_to_string( - cause.other, details_for_cause, None, None - ), - incompatibility_string, - ), + f"Because {reason}, {incompatibility_string}.", numbered=numbered, ) diff --git a/src/poetry/mixology/incompatibility.py b/src/poetry/mixology/incompatibility.py index 586343e1920..daccc348d9b 100644 --- a/src/poetry/mixology/incompatibility.py +++ b/src/poetry/mixology/incompatibility.py @@ -130,9 +130,7 @@ def __str__(self) -> str: assert depender.is_positive() assert not dependee.is_positive() - return "{} depends on {}".format( - self._terse(depender, allow_every=True), self._terse(dependee) - ) + return f"{self._terse(depender, allow_every=True)} depends on {self._terse(dependee)}" elif isinstance(self._cause, PythonCause): assert len(self._terms) == 1 assert self._terms[0].is_positive() @@ -155,9 +153,7 @@ def __str__(self) -> str: assert len(self._terms) == 1 assert self._terms[0].is_positive() - return "no versions of {} match {}".format( - self._terms[0].dependency.name, self._terms[0].constraint - ) + return f"no versions of {self._terms[0].dependency.name} match {self._terms[0].constraint}" elif isinstance(self._cause, PackageNotFoundCause): assert len(self._terms) == 1 assert self._terms[0].is_positive() @@ -168,18 +164,14 @@ def __str__(self) -> str: assert not self._terms[0].is_positive() assert self._terms[0].dependency.is_root - return "{} is {}".format( - self._terms[0].dependency.name, self._terms[0].dependency.constraint - ) + return f"{self._terms[0].dependency.name} is {self._terms[0].dependency.constraint}" elif self.is_failure(): return "version solving failed" if len(self._terms) == 1: term = self._terms[0] - return "{} is {}".format( - term.dependency.name, - "forbidden" if term.is_positive() else "required", - ) + verb = "forbidden" if term.is_positive() else "required" + return f"{term.dependency.name} is {verb}" if len(self._terms) == 2: term1 = self._terms[0] @@ -200,9 +192,7 @@ def __str__(self) -> str: return f"{package1} is incompatible with {package2}" else: - return "either {} or {}".format( - self._terse(term1), self._terse(term2) - ) + return f"either {self._terse(term1)} or {self._terse(term2)}" positive = [] negative = [] @@ -217,17 +207,13 @@ def __str__(self) -> str: if len(positive) == 1: positive_term = [term for term in self._terms if term.is_positive()][0] - return "{} requires {}".format( - self._terse(positive_term, allow_every=True), " or ".join(negative) - ) + return f"{self._terse(positive_term, allow_every=True)} requires {' or '.join(negative)}" else: - return "if {} then {}".format( - " and ".join(positive), " or ".join(negative) - ) + return f"if {' and '.join(positive)} then {' or '.join(negative)}" elif positive: - return "one of {} must be false".format(" or ".join(positive)) + return f"one of {' or '.join(positive)} must be false" else: - return "one of {} must be true".format(" or ".join(negative)) + return f"one of {' or '.join(negative)} must be true" def and_to_string( self, @@ -254,12 +240,12 @@ def and_to_string( buffer = [str(self)] if this_line is not None: - buffer.append(" " + str(this_line)) + buffer.append(f" {this_line!s}") - buffer.append(f" and {str(other)}") + buffer.append(f" and {other!s}") if other_line is not None: - buffer.append(" " + str(other_line)) + buffer.append(f" {other_line!s}") return "\n".join(buffer) @@ -454,9 +440,7 @@ def _terse(self, term: "Term", allow_every: bool = False) -> str: if term.dependency.is_root: return term.dependency.pretty_name - return "{} ({})".format( - term.dependency.pretty_name, term.dependency.pretty_constraint - ) + return f"{term.dependency.pretty_name} ({term.dependency.pretty_constraint})" def _single_term_where( self, callable: Callable[["Term"], bool] @@ -474,4 +458,4 @@ def _single_term_where( return found def __repr__(self) -> str: - return f"" + return f"" diff --git a/src/poetry/mixology/solutions/solutions/python_requirement_solution.py b/src/poetry/mixology/solutions/solutions/python_requirement_solution.py index 25eeb435fc8..4734f6c83de 100644 --- a/src/poetry/mixology/solutions/solutions/python_requirement_solution.py +++ b/src/poetry/mixology/solutions/solutions/python_requirement_solution.py @@ -26,11 +26,9 @@ def __init__(self, exception: "PackageNotFoundCause") -> None: constraint = parse_constraint(incompatibility.cause.python_version) version_solutions.append( - "For {}, a possible solution would be " - 'to set the `python` property to "{}"'.format( - incompatibility.terms[0].dependency.name, - root_constraint.intersect(constraint), - ) + f"For {incompatibility.terms[0].dependency.name}, " + "a possible solution would be to set the `python` " + f'property to "{root_constraint.intersect(constraint)}"' ) description = ( diff --git a/src/poetry/mixology/term.py b/src/poetry/mixology/term.py index 44838ba20e0..df30ce621ed 100644 --- a/src/poetry/mixology/term.py +++ b/src/poetry/mixology/term.py @@ -162,7 +162,8 @@ def _non_empty_term( return Term(self.dependency.with_constraint(constraint), is_positive) def __str__(self) -> str: - return "{}{}".format("not " if not self.is_positive() else "", self._dependency) + prefix = "not " if not self.is_positive() else "" + return f"{prefix}{self._dependency}" def __repr__(self) -> str: - return f"" + return f"" diff --git a/src/poetry/mixology/version_solver.py b/src/poetry/mixology/version_solver.py index 19cc98a6cf5..4d0dd961c0b 100644 --- a/src/poetry/mixology/version_solver.py +++ b/src/poetry/mixology/version_solver.py @@ -87,10 +87,8 @@ def solve(self) -> SolverResult: raise finally: self._log( - "Version solving took {:.3f} seconds.\n" - "Tried {} solutions.".format( - time.time() - start, self._solution.attempted_solutions - ) + f"Version solving took {time.time() - start:.3f} seconds.\n" + f"Tried {self._solution.attempted_solutions} solutions." ) def _propagate(self, package: str) -> None: @@ -172,11 +170,8 @@ def _propagate_incompatibility( if unsatisfied is None: return _conflict - self._log( - "derived: {}{}".format( - "not " if unsatisfied.is_positive() else "", unsatisfied.dependency - ) - ) + adverb = "not " if unsatisfied.is_positive() else "" + self._log(f"derived: {adverb}{unsatisfied.dependency}") self._solution.derive( unsatisfied.dependency, not unsatisfied.is_positive(), incompatibility @@ -304,14 +299,11 @@ def _resolve_conflict(self, incompatibility: Incompatibility) -> Incompatibility new_incompatibility = True partially = "" if difference is None else " partially" - bang = "!" self._log( - "{} {} is{} satisfied by {}".format( - bang, most_recent_term, partially, most_recent_satisfier - ) + f"! {most_recent_term} is{partially} satisfied by {most_recent_satisfier}" ) - self._log(f'{bang} which is caused by "{most_recent_satisfier.cause}"') - self._log(f"{bang} thus: {incompatibility}") + self._log(f'! which is caused by "{most_recent_satisfier.cause}"') + self._log(f"! thus: {incompatibility}") raise SolveFailure(incompatibility) @@ -412,9 +404,7 @@ def _get_min(dependency: Dependency) -> Tuple[bool, int]: if not conflict: self._solution.decide(version) self._log( - "selecting {} ({})".format( - version.complete_name, version.full_pretty_version - ) + f"selecting {version.complete_name} ({version.full_pretty_version})" ) return dependency.complete_name diff --git a/src/poetry/publishing/publisher.py b/src/poetry/publishing/publisher.py index 933366feb8d..309c2a951f3 100644 --- a/src/poetry/publishing/publisher.py +++ b/src/poetry/publishing/publisher.py @@ -67,9 +67,7 @@ def publish( auth = self._authenticator.get_http_auth(repository_name) if auth: logger.debug( - "Found authentication information for {}.".format( - repository_name - ) + f"Found authentication information for {repository_name}." ) username = auth["username"] password = auth["password"] @@ -88,13 +86,11 @@ def publish( self._uploader.auth(username, password) + if repository_name == "pypi": + repository_name = "PyPI" self._io.write_line( - "Publishing {} ({}) " - "to {}".format( - self._package.pretty_name, - self._package.pretty_version, - "PyPI" if repository_name == "pypi" else repository_name, - ) + f"Publishing {self._package.pretty_name} ({self._package.pretty_version}) " + f"to {repository_name}" ) self._uploader.upload( diff --git a/src/poetry/publishing/uploader.py b/src/poetry/publishing/uploader.py index 27b1e8cbe40..6decfc45237 100644 --- a/src/poetry/publishing/uploader.py +++ b/src/poetry/publishing/uploader.py @@ -335,4 +335,4 @@ def _get_type(self, file: "Path") -> str: elif len(exts) >= 2 and "".join(exts[-2:]) == ".tar.gz": return "sdist" - raise ValueError("Unknown distribution format {}".format("".join(exts))) + raise ValueError("Unknown distribution format " + "".join(exts)) diff --git a/src/poetry/puzzle/provider.py b/src/poetry/puzzle/provider.py index ae2adeaea13..4413a880142 100644 --- a/src/poetry/puzzle/provider.py +++ b/src/poetry/puzzle/provider.py @@ -216,9 +216,8 @@ def get_package_from_vcs( if vcs != "git": raise ValueError(f"Unsupported VCS dependency {vcs}") - tmp_dir = Path( - mkdtemp(prefix="pypoetry-git-{}".format(url.split("/")[-1].rstrip(".git"))) - ) + suffix = url.split("/")[-1].rstrip(".git") + tmp_dir = Path(mkdtemp(prefix=f"pypoetry-git-{suffix}")) try: git = Git() @@ -259,9 +258,7 @@ def search_for_file(self, dependency: "FileDependency") -> List["Package"]: if dependency.name != package.name: # For now, the dependency's name must match the actual package's name raise RuntimeError( - "The dependency name for {} does not match the actual package's name: {}".format( - dependency.name, package.name - ) + f"The dependency name for {dependency.name} does not match the actual package's name: {package.name}" ) if dependency.base is not None: @@ -321,9 +318,7 @@ def get_package_from_directory( if name and name != package.name: # For now, the dependency's name must match the actual package's name raise RuntimeError( - "The dependency name for {} does not match the actual package's name: {}".format( - name, package.name - ) + f"The dependency name for {name} does not match the actual package's name: {package.name}" ) return package @@ -337,9 +332,7 @@ def search_for_url(self, dependency: "URLDependency") -> List["Package"]: if dependency.name != package.name: # For now, the dependency's name must match the actual package's name raise RuntimeError( - "The dependency name for {} does not match the actual package's name: {}".format( - dependency.name, package.name - ) + f"The dependency name for {dependency.name} does not match the actual package's name: {package.name}" ) for extra in dependency.extras: @@ -596,7 +589,7 @@ def complete_package(self, package: DependencyPackage) -> DependencyPackage: continue if len(by_constraint) == 1: - self.debug(f"Merging requirements for {str(deps[0])}") + self.debug(f"Merging requirements for {deps[0]!s}") dependencies.append(list(by_constraint.values())[0][0]) continue @@ -633,28 +626,19 @@ def complete_package(self, package: DependencyPackage) -> DependencyPackage: # with the following overrides: # - {=2.0)>} # - {} - markers = [] - for deps in by_constraint.values(): - markers.append(deps[0].marker) - _deps = [_dep[0] for _dep in by_constraint.values()] - self.debug( - "Different requirements found for {}.".format( - ", ".join( - "{} ({}) with markers {}".format( - d.name, - d.pretty_constraint, - d.marker if not d.marker.is_any() else "*", - ) - for d in _deps[:-1] - ) - + " and " - + "{} ({}) with markers {}".format( - _deps[-1].name, - _deps[-1].pretty_constraint, - _deps[-1].marker if not _deps[-1].marker.is_any() else "*", - ) + + def fmt_warning(d: "Dependency") -> str: + marker = d.marker if not d.marker.is_any() else "*" + return ( + f"{d.name} ({d.pretty_constraint}) " + f"with markers {marker}" ) + + warnings = ", ".join(fmt_warning(d) for d in _deps[:-1]) + warnings += f" and {fmt_warning(_deps[-1])}" + self.debug( + f"Different requirements found for {warnings}." ) # We need to check if one of the duplicate dependencies @@ -758,10 +742,8 @@ def debug(self, message: str, depth: int = 0) -> None: version = "" message = ( - "fact: {}{} " - "depends on {} ({})".format( - name, version, m.group(2), m.group(3) - ) + f"fact: {name}{version} " + f"depends on {m.group(2)} ({m.group(3)})" ) elif " is " in message: message = re.sub( @@ -773,7 +755,7 @@ def debug(self, message: str, depth: int = 0) -> None: message = re.sub( r"(?<=: )(.+?) \((.+?)\)", "\\1 (\\2)", message ) - message = "fact: {}".format(message.split("fact: ")[1]) + message = f"fact: {message.split('fact: ')[1]}" elif message.startswith("selecting "): message = re.sub( r"selecting (.+?) \((.+?)\)", @@ -783,12 +765,10 @@ def debug(self, message: str, depth: int = 0) -> None: elif message.startswith("derived:"): m = re.match(r"derived: (.+?) \((.+?)\)$", message) if m: - message = "derived: {} ({})".format( - m.group(1), m.group(2) - ) + message = f"derived: {m.group(1)} ({m.group(2)})" else: - message = "derived: {}".format( - message.split("derived: ")[1] + message = ( + f"derived: {message.split('derived: ')[1]}" ) elif message.startswith("conflict:"): m = re.match(r"conflict: (.+?) depends on (.+?) \((.+?)\)", message) @@ -802,15 +782,11 @@ def debug(self, message: str, depth: int = 0) -> None: version = "" message = ( - "conflict: {}{} " - "depends on {} ({})".format( - name, version, m.group(2), m.group(3) - ) + f"conflict: {name}{version} " + f"depends on {m.group(2)} ({m.group(3)})" ) else: - message = "conflict: {}".format( - message.split("conflict: ")[1] - ) + message = f"conflict: {message.split('conflict: ')[1]}" message = message.replace("! ", "! ") diff --git a/src/poetry/puzzle/solver.py b/src/poetry/puzzle/solver.py index e49f0d0e3c1..c9640daa5f7 100644 --- a/src/poetry/puzzle/solver.py +++ b/src/poetry/puzzle/solver.py @@ -78,9 +78,7 @@ def solve(self, use_latest: List[str] = None) -> "Transaction": if len(self._overrides) > 1: self._provider.debug( - "Complete version solving took {:.3f} seconds with {} overrides".format( - end - start, len(self._overrides) - ) + f"Complete version solving took {end - start:.3f} seconds with {len(self._overrides)} overrides" ) self._provider.debug( f"Resolved with overrides: {', '.join(f'({b})' for b in self._overrides)}" diff --git a/src/poetry/repositories/legacy_repository.py b/src/poetry/repositories/legacy_repository.py index 217265cfb26..cd147914c81 100644 --- a/src/poetry/repositories/legacy_repository.py +++ b/src/poetry/repositories/legacy_repository.py @@ -145,7 +145,7 @@ def clean_link(self, url: str) -> str: """Makes sure a link is fully encoded. That is, if a ' ' shows up in the link, it will be rewritten to %20 (while not over-quoting % or other characters).""" - return self._clean_re.sub(lambda match: "%%%2x" % ord(match.group(0)), url) + return self._clean_re.sub(lambda match: f"%{match.group(0):2x}", url) # TODO: revisit whether the LegacyRepository should inherit from PyPiRepository. @@ -217,14 +217,10 @@ def authenticated_url(self) -> str: return self.url parsed = urllib.parse.urlparse(self.url) + username = quote(self._session.auth.username, safe="") + password = quote(self._session.auth.password, safe="") - return "{scheme}://{username}:{password}@{netloc}{path}".format( - scheme=parsed.scheme, - username=quote(self._session.auth.username, safe=""), - password=quote(self._session.auth.password, safe=""), - netloc=parsed.netloc, - path=parsed.path, - ) + return f"{parsed.scheme}://{username}:{password}@{parsed.netloc}{parsed.path}" def find_packages(self, dependency: "Dependency") -> List[Package]: packages = [] @@ -247,14 +243,14 @@ def find_packages(self, dependency: "Dependency") -> List[Package]: key = dependency.name if not constraint.is_any(): - key = f"{key}:{str(constraint)}" + key = f"{key}:{constraint!s}" ignored_pre_release_versions = [] if self._cache.store("matches").has(key): versions = self._cache.store("matches").get(key) else: - page = self._get_page("/{}/".format(dependency.name.replace(".", "-"))) + page = self._get_page(f"/{dependency.name.replace('.', '-')}/") if page is None: return [] @@ -284,9 +280,7 @@ def find_packages(self, dependency: "Dependency") -> List[Package]: packages.append(package) self._log( - "{} packages found for {} {}".format( - len(packages), dependency.name, str(constraint) - ), + f"{len(packages)} packages found for {dependency.name} {constraint!s}", level="debug", ) @@ -323,14 +317,14 @@ def package( return package def find_links_for_package(self, package: Package) -> List[Link]: - page = self._get_page("/{}/".format(package.name.replace(".", "-"))) + page = self._get_page(f"/{package.name.replace('.', '-')}/") if page is None: return [] return list(page.links_for_version(package.version)) def _get_release_info(self, name: str, version: str) -> dict: - page = self._get_page("/{}/".format(canonicalize_name(name).replace(".", "-"))) + page = self._get_page(f"/{canonicalize_name(name).replace('.', '-')}/") if page is None: raise PackageNotFound(f'No package named "{name}"') @@ -348,9 +342,7 @@ def _get_release_info(self, name: str, version: str) -> dict: links = list(page.links_for_version(Version.parse(version))) if not links: raise PackageNotFound( - 'No valid distribution links found for package: "{}" version: "{}"'.format( - name, version - ) + f'No valid distribution links found for package: "{name}" version: "{version}"' ) urls = defaultdict(list) files = [] @@ -388,9 +380,7 @@ def _get_release_info(self, name: str, version: str) -> dict: required_hash.update(chunk) if not known_hash or known_hash.hexdigest() == link.hash: - file_hash = "{}:{}".format( - required_hash.name, required_hash.hexdigest() - ) + file_hash = f"{required_hash.name}:{required_hash.hexdigest()}" files.append({"file": link.filename, "hash": file_hash}) @@ -422,9 +412,7 @@ def _get_page(self, endpoint: str) -> Optional[Page]: if response.url != url: self._log( - "Response URL {response_url} differs from request URL {url}".format( - response_url=response.url, url=url - ), + f"Response URL {response.url} differs from request URL {url}", level="debug", ) diff --git a/src/poetry/repositories/pypi_repository.py b/src/poetry/repositories/pypi_repository.py index 6f06c8c0f5b..464ad9c7ae1 100644 --- a/src/poetry/repositories/pypi_repository.py +++ b/src/poetry/repositories/pypi_repository.py @@ -109,7 +109,7 @@ def find_packages(self, dependency: Dependency) -> List[Package]: info = self.get_package_info(dependency.name) except PackageNotFound: self._log( - f"No packages found for {dependency.name} {str(constraint)}", + f"No packages found for {dependency.name} {constraint!s}", level="debug", ) return [] @@ -121,9 +121,7 @@ def find_packages(self, dependency: Dependency) -> List[Package]: if not release: # Bad release self._log( - "No release information found for {}-{}, skipping".format( - dependency.name, version - ), + f"No release information found for {dependency.name}-{version}, skipping", level="debug", ) continue @@ -132,9 +130,7 @@ def find_packages(self, dependency: Dependency) -> List[Package]: package = Package(info["info"]["name"], version) except InvalidVersion: self._log( - 'Unable to parse version "{}" for the {} package, skipping'.format( - version, dependency.name - ), + f'Unable to parse version "{version}" for the {dependency.name} package, skipping', level="debug", ) continue @@ -149,9 +145,7 @@ def find_packages(self, dependency: Dependency) -> List[Package]: packages.append(package) self._log( - "{} packages found for {} {}".format( - len(packages), dependency.name, str(constraint) - ), + f"{len(packages)} packages found for {dependency.name} {constraint!s}", level="debug", ) @@ -189,9 +183,7 @@ def search(self, query: str) -> List[Package]: results.append(result) except InvalidVersion: self._log( - 'Unable to parse version "{}" for the {} package, skipping'.format( - version, name - ), + f'Unable to parse version "{version}" for the {name} package, skipping', level="debug", ) @@ -254,7 +246,7 @@ def find_links_for_package(self, package: Package) -> List[Link]: links = [] for url in json_data["urls"]: - h = "sha256={}".format(url["digests"]["sha256"]) + h = f"sha256={url['digests']['sha256']}" links.append(Link(url["url"] + "#" + h)) return links @@ -433,14 +425,9 @@ def _get_info_from_urls(self, urls: Dict[str, List[str]]) -> "PackageInfo": def _get_info_from_wheel(self, url: str) -> "PackageInfo": from poetry.inspection.info import PackageInfo - self._log( - "Downloading wheel: {}".format( - urllib.parse.urlparse(url).path.rsplit("/")[-1] - ), - level="debug", - ) - - filename = os.path.basename(urllib.parse.urlparse(url).path.rsplit("/")[-1]) + wheel_name = urllib.parse.urlparse(url).path.rsplit("/")[-1] + self._log(f"Downloading wheel: {wheel_name}", level="debug") + filename = os.path.basename(wheel_name) with temporary_directory() as temp_dir: filepath = Path(temp_dir) / filename @@ -451,14 +438,9 @@ def _get_info_from_wheel(self, url: str) -> "PackageInfo": def _get_info_from_sdist(self, url: str) -> "PackageInfo": from poetry.inspection.info import PackageInfo - self._log( - "Downloading sdist: {}".format( - urllib.parse.urlparse(url).path.rsplit("/")[-1] - ), - level="debug", - ) - - filename = os.path.basename(urllib.parse.urlparse(url).path) + sdist_name = urllib.parse.urlparse(url).path + self._log(f"Downloading sdist: {sdist_name.rsplit('/')[-1]}", level="debug") + filename = os.path.basename(sdist_name) with temporary_directory() as temp_dir: filepath = Path(temp_dir) / filename diff --git a/src/poetry/utils/authenticator.py b/src/poetry/utils/authenticator.py index 3e2d8285e95..85f7dadc955 100644 --- a/src/poetry/utils/authenticator.py +++ b/src/poetry/utils/authenticator.py @@ -35,11 +35,7 @@ def __init__(self, config: "Config", io: Optional["IO"] = None) -> None: def _log(self, message: str, level: str = "debug") -> None: if self._io is not None: - self._io.write_line( - "<{level:s}>{message:s}".format( - message=message, level=level - ) - ) + self._io.write_line(f"<{level}>{message}") else: getattr(logger, level, logger.debug)(message) diff --git a/src/poetry/utils/env.py b/src/poetry/utils/env.py index aaaf89270da..5eadf3cdfd2 100644 --- a/src/poetry/utils/env.py +++ b/src/poetry/utils/env.py @@ -254,19 +254,16 @@ def make_candidates( except ValueError: pass else: + site_type = "writable " if writable_only else "" raise ValueError( - "{} is not relative to any discovered {}sites".format( - path, "writable " if writable_only else "" - ) + f"{path} is not relative to any discovered {site_type}sites" ) results = [candidate / path for candidate in candidates if candidate] if not results and strict: raise RuntimeError( - 'Unable to find a suitable destination for "{}" in {}'.format( - str(path), paths_csv(self._candidates) - ) + f'Unable to find a suitable destination for "{path}" in {paths_csv(self._candidates)}' ) return results @@ -421,9 +418,7 @@ class EnvCommandError(EnvError): def __init__(self, e: CalledProcessError, input: Optional[str] = None) -> None: self.e = e - message = "Command {} errored with the following return code {}, and output: \n{}".format( - e.cmd, e.returncode, decode(e.output) - ) + message = f"Command {e.cmd} errored with the following return code {e.returncode}, and output: \n{decode(e.output)}" if input: message += f"input was : {input}" super().__init__(message) @@ -433,11 +428,11 @@ class NoCompatiblePythonVersionFound(EnvError): def __init__(self, expected: str, given: Optional[str] = None) -> None: if given: message = ( - "The specified Python version ({}) " - "is not supported by the project ({}).\n" + f"The specified Python version ({given}) " + f"is not supported by the project ({expected}).\n" "Please choose a compatible version " "or loosen the python constraint specified " - "in the pyproject.toml file.".format(given, expected) + "in the pyproject.toml file." ) else: message = ( @@ -573,11 +568,8 @@ def deactivate(self, io: "IO") -> None: envs = envs_file.read() env = envs.get(name) if env is not None: - io.write_line( - "Deactivating virtualenv: {}".format( - venv_path / (name + "-py{}".format(env["minor"])) - ) - ) + venv = venv_path / f"{name}-py{env['minor']}" + io.write_line(f"Deactivating virtualenv: {venv}") del envs[name] envs_file.write(envs) @@ -834,11 +826,9 @@ def create_venv( ) io.write_line( - "The currently activated Python version {} " - "is not supported by the project ({}).\n" - "Trying to find and use a compatible version. ".format( - python_patch, self._poetry.package.python_versions - ) + f"The currently activated Python version {python_patch} " + f"is not supported by the project ({self._poetry.package.python_versions}).\n" + "Trying to find and use a compatible version. " ) for python_to_try in sorted( @@ -910,17 +900,15 @@ def create_venv( return self.get_system_env() - io.write_line(f"Creating virtualenv {name} in {str(venv_path)}") + io.write_line(f"Creating virtualenv {name} in {venv_path!s}") else: create_venv = False if force: if not env.is_sane(): io.write_line( - "The virtual environment found in {} seems to be broken.".format( - env.path - ) + f"The virtual environment found in {env.path} seems to be broken." ) - io.write_line(f"Recreating virtualenv {name} in {str(venv)}") + io.write_line(f"Recreating virtualenv {name} in {venv!s}") self.remove_venv(venv) create_venv = True elif io.is_very_verbose(): @@ -1493,7 +1481,7 @@ def get_supported_tags(self) -> List[Tag]: def get_marker_env(self) -> Dict[str, Any]: if hasattr(sys, "implementation"): info = sys.implementation.version - iver = "{0.major}.{0.minor}.{0.micro}".format(info) + iver = f"{info.major}.{info.minor}.{info.micro}" kind = info.releaselevel if kind != "final": iver += kind[0] + str(info.serial) @@ -1678,8 +1666,8 @@ def find_executables(self) -> None: patterns = [("python*", "pip*")] if self._child_env: - minor_version = "{}.{}".format( - self._child_env.version_info[0], self._child_env.version_info[1] + minor_version = ( + f"{self._child_env.version_info[0]}.{self._child_env.version_info[1]}" ) major_version = f"{self._child_env.version_info[0]}" patterns = [ diff --git a/src/poetry/utils/exporter.py b/src/poetry/utils/exporter.py index 73a1c2ebb3c..08a3c8dd60a 100644 --- a/src/poetry/utils/exporter.py +++ b/src/poetry/utils/exporter.py @@ -45,7 +45,7 @@ def export( if fmt not in self.ACCEPTED_FORMATS: raise ValueError(f"Invalid export format: {fmt}") - getattr(self, "_export_{}".format(fmt.replace(".", "_")))( + getattr(self, "_export_" + fmt.replace(".", "_"))( cwd, output, with_hashes=with_hashes, @@ -120,11 +120,8 @@ def _export_requirements_txt( hashes.append(f"{algorithm}:{h}") if hashes: - line += " \\\n" - for i, h in enumerate(hashes): - line += " --hash={}{}".format( - h, " \\\n" if i < len(hashes) - 1 else "" - ) + sep = " \\\n" + line += sep + sep.join(f" --hash={h}" for h in hashes) dependency_lines.add(line) content += "\n".join(sorted(dependency_lines)) diff --git a/src/poetry/utils/helpers.py b/src/poetry/utils/helpers.py index 36223ff9c67..4f7cf114e55 100644 --- a/src/poetry/utils/helpers.py +++ b/src/poetry/utils/helpers.py @@ -110,16 +110,14 @@ def get_package_version_display_string( package: "Package", root: Optional[Path] = None ) -> str: if package.source_type in ["file", "directory"] and root: - return "{} {}".format( - package.version, - Path(os.path.relpath(package.source_url, root.as_posix())).as_posix(), - ) + path = Path(os.path.relpath(package.source_url, root.as_posix())).as_posix() + return f"{package.version} {path}" return package.full_pretty_version def paths_csv(paths: List[Path]) -> str: - return ", ".join(f'"{str(c)}"' for c in paths) + return ", ".join(f'"{c!s}"' for c in paths) def is_dir_writable(path: Path, create: bool = False) -> bool: @@ -135,3 +133,9 @@ def is_dir_writable(path: Path, create: bool = False) -> bool: return False else: return True + + +def pluralize(count: int, word: str = "") -> str: + if count == 1: + return word + return word + "s" diff --git a/src/poetry/utils/password_manager.py b/src/poetry/utils/password_manager.py index f2bde4f9fa5..17be50248ae 100644 --- a/src/poetry/utils/password_manager.py +++ b/src/poetry/utils/password_manager.py @@ -61,9 +61,7 @@ def set_password(self, name: str, username: str, password: str) -> None: keyring.set_password(name, username, password) except (RuntimeError, keyring.errors.KeyringError) as e: raise KeyRingError( - "Unable to store the password for {} in the key ring: {}".format( - name, str(e) - ) + f"Unable to store the password for {name} in the key ring: {e}" ) def delete_password(self, name: str, username: str) -> None: @@ -89,7 +87,7 @@ def _check(self) -> None: try: import keyring except Exception as e: - logger.debug(f"An error occurred while importing keyring: {str(e)}") + logger.debug(f"An error occurred while importing keyring: {e!s}") self._is_available = False return diff --git a/src/poetry/utils/setup_reader.py b/src/poetry/utils/setup_reader.py index ab9d7d11d54..f8578658058 100644 --- a/src/poetry/utils/setup_reader.py +++ b/src/poetry/utils/setup_reader.py @@ -41,9 +41,8 @@ def read_from_directory( if not filepath.exists(): continue - new_result = getattr(cls(), "read_{}".format(filename.replace(".", "_")))( - filepath - ) + read_file_func = getattr(cls(), "read_" + filename.replace(".", "_")) + new_result = read_file_func(filepath) for key in result.keys(): if new_result[key]: diff --git a/tests/conftest.py b/tests/conftest.py index ef5b5009a99..a1b4d486e57 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -309,7 +309,7 @@ def current_python(current_env: SystemEnv) -> Tuple[int, int, int]: @pytest.fixture(scope="session") def default_python(current_python: Tuple[int, int, int]) -> str: - return "^{}".format(".".join(str(v) for v in current_python[:2])) + return "^" + ".".join(str(v) for v in current_python[:2]) @pytest.fixture diff --git a/tests/console/commands/env/test_info.py b/tests/console/commands/env/test_info.py index 43a7bb6ab4d..276ff93ca80 100644 --- a/tests/console/commands/env/test_info.py +++ b/tests/console/commands/env/test_info.py @@ -33,27 +33,21 @@ def tester(command_tester_factory: "CommandTesterFactory") -> "CommandTester": def test_env_info_displays_complete_info(tester: "CommandTester"): tester.execute() - expected = """ + expected = f""" Virtualenv Python: 3.7.0 Implementation: CPython -Path: {prefix} -Executable: {executable} +Path: {Path('/prefix')} +Executable: {sys.executable} Valid: True System Platform: darwin OS: posix -Python: {base_version} -Path: {base_prefix} -Executable: {base_executable} -""".format( - prefix=str(Path("/prefix")), - base_prefix=str(Path("/base/prefix")), - base_version=".".join(str(v) for v in sys.version_info[:3]), - executable=sys.executable, - base_executable="python", - ) +Python: {'.'.join(str(v) for v in sys.version_info[:3])} +Path: {Path('/base/prefix')} +Executable: python +""" assert expected == tester.io.fetch_output() diff --git a/tests/console/commands/env/test_use.py b/tests/console/commands/env/test_use.py index eb6dcfd9be1..6e7be093387 100644 --- a/tests/console/commands/env/test_use.py +++ b/tests/console/commands/env/test_use.py @@ -82,14 +82,10 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file( assert envs[venv_name]["minor"] == "3.7" assert envs[venv_name]["patch"] == "3.7.1" - expected = """\ -Creating virtualenv {} in {} -Using virtualenv: {} -""".format( - venv_py37.name, - venv_py37.parent, - venv_py37, - ) + expected = f"""\ +Creating virtualenv {venv_py37.name} in {venv_py37.parent} +Using virtualenv: {venv_py37} +""" assert expected == tester.io.fetch_output() @@ -115,11 +111,9 @@ def test_get_prefers_explicitly_activated_virtualenvs_over_env_var( tester.execute(python_minor) - expected = """\ -Using virtualenv: {} -""".format( - venv_dir - ) + expected = f"""\ +Using virtualenv: {venv_dir} +""" assert expected == tester.io.fetch_output() @@ -151,13 +145,9 @@ def test_get_prefers_explicitly_activated_non_existing_virtualenvs_over_env_var( tester.execute(python_minor) - expected = """\ -Creating virtualenv {} in {} -Using virtualenv: {} -""".format( - venv_dir.name, - venv_dir.parent, - venv_dir, - ) + expected = f"""\ +Creating virtualenv {venv_dir.name} in {venv_dir.parent} +Using virtualenv: {venv_dir} +""" assert expected == tester.io.fetch_output() diff --git a/tests/console/commands/self/test_update.py b/tests/console/commands/self/test_update.py index 6e1882d02bd..33c60dbf39c 100644 --- a/tests/console/commands/self/test_update.py +++ b/tests/console/commands/self/test_update.py @@ -70,7 +70,7 @@ def test_self_update_can_update_from_recommended_installation( tester.execute() - expected_output = """\ + expected_output = f"""\ Updating Poetry to 1.2.0 Updating dependencies @@ -79,14 +79,12 @@ def test_self_update_can_update_from_recommended_installation( Package operations: 0 installs, 2 updates, 0 removals - Updating cleo (0.8.2 -> 1.0.0) - - Updating poetry ({} -> {}) + - Updating poetry ({__version__} -> {new_version}) Updating the poetry script -Poetry ({}) is installed now. Great! -""".format( - __version__, new_version, new_version - ) +Poetry ({new_version}) is installed now. Great! +""" assert tester.io.fetch_output() == expected_output diff --git a/tests/console/commands/test_add.py b/tests/console/commands/test_add.py index 9961edf4a09..841b44fb565 100644 --- a/tests/console/commands/test_add.py +++ b/tests/console/commands/test_add.py @@ -366,7 +366,7 @@ def test_add_directory_constraint( path = "../git/github.com/demo/demo" tester.execute(f"{path}" if not editable else f"-e {path}") - expected = """\ + expected = f"""\ Updating dependencies Resolving dependencies... @@ -376,10 +376,8 @@ def test_add_directory_constraint( Package operations: 2 installs, 0 updates, 0 removals • Installing pendulum (1.4.4) - • Installing demo (0.1.2 {}) -""".format( - app.poetry.file.parent.joinpath(path).resolve().as_posix() - ) + • Installing demo (0.1.2 {app.poetry.file.parent.joinpath(path).resolve().as_posix()}) +""" assert expected == tester.io.fetch_output() assert tester.command.installer.executor.installations_count == 2 @@ -409,7 +407,7 @@ def test_add_directory_with_poetry( path = "../git/github.com/demo/pyproject-demo" tester.execute(f"{path}") - expected = """\ + expected = f"""\ Updating dependencies Resolving dependencies... @@ -419,10 +417,8 @@ def test_add_directory_with_poetry( Package operations: 2 installs, 0 updates, 0 removals • Installing pendulum (1.4.4) - • Installing demo (0.1.2 {}) -""".format( - app.poetry.file.parent.joinpath(path).resolve().as_posix() - ) + • Installing demo (0.1.2 {app.poetry.file.parent.joinpath(path).resolve().as_posix()}) +""" assert expected == tester.io.fetch_output() assert tester.command.installer.executor.installations_count == 2 @@ -443,7 +439,7 @@ def test_add_file_constraint_wheel( path = "../distributions/demo-0.1.0-py2.py3-none-any.whl" tester.execute(f"{path}") - expected = """\ + expected = f"""\ Updating dependencies Resolving dependencies... @@ -453,10 +449,8 @@ def test_add_file_constraint_wheel( Package operations: 2 installs, 0 updates, 0 removals • Installing pendulum (1.4.4) - • Installing demo (0.1.0 {}) -""".format( - app.poetry.file.parent.joinpath(path).resolve().as_posix() - ) + • Installing demo (0.1.0 {app.poetry.file.parent.joinpath(path).resolve().as_posix()}) +""" assert expected == tester.io.fetch_output() assert tester.command.installer.executor.installations_count == 2 @@ -483,7 +477,7 @@ def test_add_file_constraint_sdist( path = "../distributions/demo-0.1.0.tar.gz" tester.execute(f"{path}") - expected = """\ + expected = f"""\ Updating dependencies Resolving dependencies... @@ -493,10 +487,8 @@ def test_add_file_constraint_sdist( Package operations: 2 installs, 0 updates, 0 removals • Installing pendulum (1.4.4) - • Installing demo (0.1.0 {}) -""".format( - app.poetry.file.parent.joinpath(path).resolve().as_posix() - ) + • Installing demo (0.1.0 {app.poetry.file.parent.joinpath(path).resolve().as_posix()}) +""" assert expected == tester.io.fetch_output() assert tester.command.installer.executor.installations_count == 2 @@ -1305,7 +1297,7 @@ def test_add_directory_constraint_old_installer( path = "../git/github.com/demo/demo" old_tester.execute(f"{path}") - expected = """\ + expected = f"""\ Updating dependencies Resolving dependencies... @@ -1315,10 +1307,8 @@ def test_add_directory_constraint_old_installer( Package operations: 2 installs, 0 updates, 0 removals - Installing pendulum (1.4.4) - - Installing demo (0.1.2 {}) -""".format( - app.poetry.file.parent.joinpath(path).resolve().as_posix() - ) + - Installing demo (0.1.2 {app.poetry.file.parent.joinpath(path).resolve().as_posix()}) +""" assert expected == old_tester.io.fetch_output() @@ -1345,7 +1335,7 @@ def test_add_directory_with_poetry_old_installer( path = "../git/github.com/demo/pyproject-demo" old_tester.execute(f"{path}") - expected = """\ + expected = f"""\ Updating dependencies Resolving dependencies... @@ -1355,10 +1345,8 @@ def test_add_directory_with_poetry_old_installer( Package operations: 2 installs, 0 updates, 0 removals - Installing pendulum (1.4.4) - - Installing demo (0.1.2 {}) -""".format( - app.poetry.file.parent.joinpath(path).resolve().as_posix() - ) + - Installing demo (0.1.2 {app.poetry.file.parent.joinpath(path).resolve().as_posix()}) +""" assert expected == old_tester.io.fetch_output() @@ -1380,7 +1368,7 @@ def test_add_file_constraint_wheel_old_installer( path = "../distributions/demo-0.1.0-py2.py3-none-any.whl" old_tester.execute(f"{path}") - expected = """\ + expected = f"""\ Updating dependencies Resolving dependencies... @@ -1390,10 +1378,8 @@ def test_add_file_constraint_wheel_old_installer( Package operations: 2 installs, 0 updates, 0 removals - Installing pendulum (1.4.4) - - Installing demo (0.1.0 {}) -""".format( - app.poetry.file.parent.joinpath(path).resolve().as_posix() - ) + - Installing demo (0.1.0 {app.poetry.file.parent.joinpath(path).resolve().as_posix()}) +""" assert expected == old_tester.io.fetch_output() @@ -1422,7 +1408,7 @@ def test_add_file_constraint_sdist_old_installer( path = "../distributions/demo-0.1.0.tar.gz" old_tester.execute(f"{path}") - expected = """\ + expected = f"""\ Updating dependencies Resolving dependencies... @@ -1432,10 +1418,8 @@ def test_add_file_constraint_sdist_old_installer( Package operations: 2 installs, 0 updates, 0 removals - Installing pendulum (1.4.4) - - Installing demo (0.1.0 {}) -""".format( - app.poetry.file.parent.joinpath(path).resolve().as_posix() - ) + - Installing demo (0.1.0 {app.poetry.file.parent.joinpath(path).resolve().as_posix()}) +""" assert expected == old_tester.io.fetch_output() diff --git a/tests/console/commands/test_cache.py b/tests/console/commands/test_cache.py index f58ef4844e3..a81b03f6640 100644 --- a/tests/console/commands/test_cache.py +++ b/tests/console/commands/test_cache.py @@ -53,12 +53,10 @@ def test_cache_list( ): tester.execute() - expected = """\ -{} -{} -""".format( - repository_one, repository_two - ) + expected = f"""\ +{repository_one} +{repository_two} +""" assert expected == tester.io.fetch_output() diff --git a/tests/console/commands/test_config.py b/tests/console/commands/test_config.py index b136ca29c73..3278b19a170 100644 --- a/tests/console/commands/test_config.py +++ b/tests/console/commands/test_config.py @@ -45,7 +45,9 @@ def test_list_displays_default_value_if_not_set( ): tester.execute("--list") - expected = """cache-dir = {cache} + cache_dir = json.dumps(str(config_cache_dir)) + venv_path = json.dumps(os.path.join("{cache-dir}", "virtualenvs")) + expected = f"""cache-dir = {cache_dir} experimental.new-installer = true installer.max-workers = null installer.parallel = true @@ -53,12 +55,8 @@ def test_list_displays_default_value_if_not_set( virtualenvs.in-project = null virtualenvs.options.always-copy = false virtualenvs.options.system-site-packages = false -virtualenvs.path = {path} # {virtualenvs} -""".format( - cache=json.dumps(str(config_cache_dir)), - path=json.dumps(os.path.join("{cache-dir}", "virtualenvs")), - virtualenvs=str(config_cache_dir / "virtualenvs"), - ) +virtualenvs.path = {venv_path} # {config_cache_dir / 'virtualenvs'} +""" assert expected == tester.io.fetch_output() @@ -70,7 +68,9 @@ def test_list_displays_set_get_setting( tester.execute("--list") - expected = """cache-dir = {cache} + cache_dir = json.dumps(str(config_cache_dir)) + venv_path = json.dumps(os.path.join("{cache-dir}", "virtualenvs")) + expected = f"""cache-dir = {cache_dir} experimental.new-installer = true installer.max-workers = null installer.parallel = true @@ -78,12 +78,8 @@ def test_list_displays_set_get_setting( virtualenvs.in-project = null virtualenvs.options.always-copy = false virtualenvs.options.system-site-packages = false -virtualenvs.path = {path} # {virtualenvs} -""".format( - cache=json.dumps(str(config_cache_dir)), - path=json.dumps(os.path.join("{cache-dir}", "virtualenvs")), - virtualenvs=str(config_cache_dir / "virtualenvs"), - ) +virtualenvs.path = {venv_path} # {config_cache_dir / 'virtualenvs'} +""" assert config.set_config_source.call_count == 0 assert expected == tester.io.fetch_output() @@ -119,7 +115,9 @@ def test_list_displays_set_get_local_setting( tester.execute("--list") - expected = """cache-dir = {cache} + cache_dir = json.dumps(str(config_cache_dir)) + venv_path = json.dumps(os.path.join("{cache-dir}", "virtualenvs")) + expected = f"""cache-dir = {cache_dir} experimental.new-installer = true installer.max-workers = null installer.parallel = true @@ -127,12 +125,8 @@ def test_list_displays_set_get_local_setting( virtualenvs.in-project = null virtualenvs.options.always-copy = false virtualenvs.options.system-site-packages = false -virtualenvs.path = {path} # {virtualenvs} -""".format( - cache=json.dumps(str(config_cache_dir)), - path=json.dumps(os.path.join("{cache-dir}", "virtualenvs")), - virtualenvs=str(config_cache_dir / "virtualenvs"), - ) +virtualenvs.path = {venv_path} # {config_cache_dir / 'virtualenvs'} +""" assert config.set_config_source.call_count == 1 assert expected == tester.io.fetch_output() diff --git a/tests/console/commands/test_init.py b/tests/console/commands/test_init.py index 122a34eb255..5624b850593 100644 --- a/tests/console/commands/test_init.py +++ b/tests/console/commands/test_init.py @@ -180,7 +180,8 @@ def test_empty_license(tester: CommandTester): ] tester.execute(inputs="\n".join(inputs)) - expected = """\ + python = ".".join(str(c) for c in sys.version_info[:2]) + expected = f"""\ [tool.poetry] name = "my-package" version = "1.2.3" @@ -191,9 +192,7 @@ def test_empty_license(tester: CommandTester): [tool.poetry.dependencies] python = "^{python}" -""".format( - python=".".join(str(c) for c in sys.version_info[:2]) - ) +""" assert expected in tester.io.fetch_output() diff --git a/tests/helpers.py b/tests/helpers.py index 4d2fd75efc6..40e84d23f9e 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -216,8 +216,6 @@ def find_packages(self, dependency: "Dependency") -> List[Package]: def find_links_for_package(self, package: Package) -> List[Link]: return [ Link( - "https://foo.bar/files/{}-{}-py2.py3-none-any.whl".format( - escape_name(package.name), escape_version(package.version.text) - ) + f"https://foo.bar/files/{escape_name(package.name)}-{escape_version(package.version.text)}-py2.py3-none-any.whl" ) ] diff --git a/tests/installation/test_chef.py b/tests/installation/test_chef.py index 3a64fe0170c..783c09fcd41 100644 --- a/tests/installation/test_chef.py +++ b/tests/installation/test_chef.py @@ -83,9 +83,7 @@ def test_get_cache_directory_for_link(config: "Config", config_cache_dir: Path): ) expected = Path( - "{}/artifacts/ba/63/13/283a3b3b7f95f05e9e6f84182d276f7bb0951d5b0cc24422b33f7a4648".format( - config_cache_dir.as_posix() - ) + f"{config_cache_dir.as_posix()}/artifacts/ba/63/13/283a3b3b7f95f05e9e6f84182d276f7bb0951d5b0cc24422b33f7a4648" ) assert expected == directory diff --git a/tests/installation/test_executor.py b/tests/installation/test_executor.py index c06c7256bdb..e93035520a7 100644 --- a/tests/installation/test_executor.py +++ b/tests/installation/test_executor.py @@ -161,18 +161,16 @@ def test_execute_executes_a_batch_of_operations( ] ) - expected = """ + expected = f""" Package operations: 4 installs, 1 update, 1 removal • Installing pytest (3.5.2) • Removing attrs (17.4.0) • Updating requests (2.18.3 -> 2.18.4) - • Installing demo (0.1.0 {}) - • Installing simple-project (1.2.3 {}) + • Installing demo (0.1.0 {file_package.source_url}) + • Installing simple-project (1.2.3 {directory_package.source_url}) • Installing demo (0.1.0 master) -""".format( - file_package.source_url, directory_package.source_url - ) +""" expected = set(expected.splitlines()) output = set(io.fetch_output().splitlines()) diff --git a/tests/masonry/builders/test_editable_builder.py b/tests/masonry/builders/test_editable_builder.py index 9b879a29f63..604f5b80fe6 100644 --- a/tests/masonry/builders/test_editable_builder.py +++ b/tests/masonry/builders/test_editable_builder.py @@ -152,42 +152,36 @@ def test_builder_installs_proper_files_for_standard_packages( assert str(dist_info.joinpath("entry_points.txt")) in records assert str(dist_info.joinpath("RECORD")) in records - baz_script = """\ -#!{python} + baz_script = f"""\ +#!{tmp_venv.python} import sys from bar import baz if __name__ == '__main__': sys.exit(baz.boom.bim()) -""".format( - python=tmp_venv.python - ) +""" assert baz_script == tmp_venv._bin_dir.joinpath("baz").read_text() - foo_script = """\ -#!{python} + foo_script = f"""\ +#!{tmp_venv.python} import sys from foo import bar if __name__ == '__main__': sys.exit(bar()) -""".format( - python=tmp_venv.python - ) +""" assert foo_script == tmp_venv._bin_dir.joinpath("foo").read_text() - fox_script = """\ -#!{python} + fox_script = f"""\ +#!{tmp_venv.python} import sys from fuz.foo import bar if __name__ == '__main__': sys.exit(bar.baz()) -""".format( - python=tmp_venv.python - ) +""" assert fox_script == tmp_venv._bin_dir.joinpath("fox").read_text() diff --git a/tests/packages/test_locker.py b/tests/packages/test_locker.py index dd42e5ee5c5..d5e915d65bb 100644 --- a/tests/packages/test_locker.py +++ b/tests/packages/test_locker.py @@ -430,18 +430,15 @@ def test_locking_legacy_repository_package_should_include_source_section( def test_locker_should_emit_warnings_if_lock_version_is_newer_but_allowed( locker: Locker, caplog: "LogCaptureFixture" ): - content = """\ + version = ".".join(Version.parse(Locker._VERSION).next_minor().text.split(".")[:2]) + content = f"""\ [metadata] lock-version = "{version}" python-versions = "~2.7 || ^3.4" content-hash = "c3d07fca33fba542ef2b2a4d75bf5b48d892d21a830e2ad9c952ba5123a52f77" [metadata.files] -""".format( - version=".".join( - Version.parse(Locker._VERSION).next_minor().text.split(".")[:2] - ) - ) +""" caplog.set_level(logging.WARNING, logger="poetry.packages.locker") locker.lock.write(tomlkit.parse(content)) @@ -524,16 +521,14 @@ def test_locker_should_neither_emit_warnings_nor_raise_error_for_lower_compatibl older_version = ".".join( [str(current_version.major), str(current_version.minor - 1)] ) - content = """\ + content = f"""\ [metadata] -lock-version = "{version}" +lock-version = "{older_version}" python-versions = "~2.7 || ^3.4" content-hash = "c3d07fca33fba542ef2b2a4d75bf5b48d892d21a830e2ad9c952ba5123a52f77" [metadata.files] -""".format( - version=older_version - ) +""" caplog.set_level(logging.WARNING, logger="poetry.packages.locker") locker.lock.write(tomlkit.parse(content)) diff --git a/tests/utils/test_authenticator.py b/tests/utils/test_authenticator.py index 3b67fb6f279..9c36f9d49c8 100644 --- a/tests/utils/test_authenticator.py +++ b/tests/utils/test_authenticator.py @@ -207,7 +207,7 @@ def test_authenticator_request_retries_on_exception( mocker: "MockerFixture", config: "Config", http: Type[httpretty.httpretty] ): sleep = mocker.patch("time.sleep") - sdist_uri = f"https://foo.bar/files/{str(uuid.uuid4())}/foo-0.1.0.tar.gz" + sdist_uri = f"https://foo.bar/files/{uuid.uuid4()!s}/foo-0.1.0.tar.gz" content = str(uuid.uuid4()) seen = [] @@ -232,7 +232,7 @@ def test_authenticator_request_raises_exception_when_attempts_exhausted( mocker: "MockerFixture", config: "Config", http: Type[httpretty.httpretty] ): sleep = mocker.patch("time.sleep") - sdist_uri = f"https://foo.bar/files/{str(uuid.uuid4())}/foo-0.1.0.tar.gz" + sdist_uri = f"https://foo.bar/files/{uuid.uuid4()!s}/foo-0.1.0.tar.gz" def callback(*_: Any, **___: Any) -> None: raise requests.exceptions.ConnectionError(str(uuid.uuid4())) @@ -267,7 +267,7 @@ def test_authenticator_request_retries_on_status_code( attempts: int, ): sleep = mocker.patch("time.sleep") - sdist_uri = f"https://foo.bar/files/{str(uuid.uuid4())}/foo-0.1.0.tar.gz" + sdist_uri = f"https://foo.bar/files/{uuid.uuid4()!s}/foo-0.1.0.tar.gz" content = str(uuid.uuid4()) def callback( diff --git a/tests/utils/test_env.py b/tests/utils/test_env.py index 8d0f82efb7d..1df3de7185a 100644 --- a/tests/utils/test_env.py +++ b/tests/utils/test_env.py @@ -470,10 +470,8 @@ def test_deactivate_non_activated_but_existing( venv_name = manager.generate_env_name("simple-project", str(poetry.file.parent)) - ( - Path(tmp_dir) - / "{}-py{}".format(venv_name, ".".join(str(c) for c in sys.version_info[:2])) - ).mkdir() + python = ".".join(str(c) for c in sys.version_info[:2]) + (Path(tmp_dir) / f"{venv_name}-py{python}").mkdir() config.merge({"virtualenvs": {"path": str(tmp_dir)}}) @@ -485,9 +483,7 @@ def test_deactivate_non_activated_but_existing( manager.deactivate(NullIO()) env = manager.get() - assert env.path == Path(tmp_dir) / "{}-py{}".format( - venv_name, ".".join(str(c) for c in sys.version_info[:2]) - ) + assert env.path == Path(tmp_dir) / f"{venv_name}-py{python}" assert Path("/prefix") @@ -502,7 +498,7 @@ def test_deactivate_activated( del os.environ["VIRTUAL_ENV"] venv_name = manager.generate_env_name("simple-project", str(poetry.file.parent)) - version = Version.parse(".".join(str(c) for c in sys.version_info[:3])) + version = Version.from_parts(*sys.version_info[:3]) other_version = Version.parse("3.4") if version.major == 2 else version.next_minor() (Path(tmp_dir) / f"{venv_name}-py{version.major}.{version.minor}").mkdir() ( @@ -527,9 +523,7 @@ def test_deactivate_activated( manager.deactivate(NullIO()) env = manager.get() - assert env.path == Path(tmp_dir) / "{}-py{}.{}".format( - venv_name, version.major, version.minor - ) + assert env.path == Path(tmp_dir) / f"{venv_name}-py{version.major}.{version.minor}" assert Path("/prefix") envs = envs_file.read() @@ -928,9 +922,9 @@ def test_create_venv_uses_patch_version_to_detect_compatibility( if "VIRTUAL_ENV" in os.environ: del os.environ["VIRTUAL_ENV"] - version = Version.parse(".".join(str(c) for c in sys.version_info[:3])) - poetry.package.python_versions = "^{}".format( - ".".join(str(c) for c in sys.version_info[:3]) + version = Version.from_parts(*sys.version_info[:3]) + poetry.package.python_versions = "^" + ".".join( + str(c) for c in sys.version_info[:3] ) venv_name = manager.generate_env_name("simple-project", str(poetry.file.parent)) @@ -966,10 +960,8 @@ def test_create_venv_uses_patch_version_to_detect_compatibility_with_executable( if "VIRTUAL_ENV" in os.environ: del os.environ["VIRTUAL_ENV"] - version = Version.parse(".".join(str(c) for c in sys.version_info[:3])) - poetry.package.python_versions = "~{}".format( - ".".join(str(c) for c in (version.major, version.minor - 1, 0)) - ) + version = Version.from_parts(*sys.version_info[:3]) + poetry.package.python_versions = f"~{version.major}.{version.minor-1}.0" venv_name = manager.generate_env_name("simple-project", str(poetry.file.parent)) check_output = mocker.patch( @@ -1087,16 +1079,10 @@ def test_env_finds_the_correct_executables(tmp_dir: str, manager: EnvManager): manager.build_venv(str(venv_path), with_pip=True) venv = VirtualEnv(venv_path) - default_executable = expected_executable = "python" + (".exe" if WINDOWS else "") - default_pip_executable = expected_pip_executable = "pip" + ( - ".exe" if WINDOWS else "" - ) - major_executable = "python{}{}".format( - sys.version_info[0], ".exe" if WINDOWS else "" - ) - major_pip_executable = "pip{}{}".format( - sys.version_info[0], ".exe" if WINDOWS else "" - ) + default_executable = expected_executable = f"python{'.exe' if WINDOWS else ''}" + default_pip_executable = expected_pip_executable = f"pip{'.exe' if WINDOWS else ''}" + major_executable = f"python{sys.version_info[0]}{'.exe' if WINDOWS else ''}" + major_pip_executable = f"pip{sys.version_info[0]}{'.exe' if WINDOWS else ''}" if ( venv._bin_dir.joinpath(default_executable).exists() @@ -1130,11 +1116,11 @@ def test_env_finds_the_correct_executables_for_generic_env( ) venv = GenericEnv(parent_venv.path, child_env=VirtualEnv(child_venv_path)) - expected_executable = "python{}.{}{}".format( - sys.version_info[0], sys.version_info[1], ".exe" if WINDOWS else "" + expected_executable = ( + f"python{sys.version_info[0]}.{sys.version_info[1]}{'.exe' if WINDOWS else ''}" ) - expected_pip_executable = "pip{}.{}{}".format( - sys.version_info[0], sys.version_info[1], ".exe" if WINDOWS else "" + expected_pip_executable = ( + f"pip{sys.version_info[0]}.{sys.version_info[1]}{'.exe' if WINDOWS else ''}" ) if WINDOWS: @@ -1157,12 +1143,10 @@ def test_env_finds_fallback_executables_for_generic_env( ) venv = GenericEnv(parent_venv.path, child_env=VirtualEnv(child_venv_path)) - default_executable = "python" + (".exe" if WINDOWS else "") - major_executable = "python{}{}".format( - sys.version_info[0], ".exe" if WINDOWS else "" - ) - minor_executable = "python{}.{}{}".format( - sys.version_info[0], sys.version_info[1], ".exe" if WINDOWS else "" + default_executable = f"python{'.exe' if WINDOWS else ''}" + major_executable = f"python{sys.version_info[0]}{'.exe' if WINDOWS else ''}" + minor_executable = ( + f"python{sys.version_info[0]}.{sys.version_info[1]}{'.exe' if WINDOWS else ''}" ) expected_executable = minor_executable if ( @@ -1179,12 +1163,10 @@ def test_env_finds_fallback_executables_for_generic_env( venv._bin_dir.joinpath(expected_executable).unlink() expected_executable = default_executable - default_pip_executable = "pip" + (".exe" if WINDOWS else "") - major_pip_executable = "pip{}{}".format( - sys.version_info[0], ".exe" if WINDOWS else "" - ) - minor_pip_executable = "pip{}.{}{}".format( - sys.version_info[0], sys.version_info[1], ".exe" if WINDOWS else "" + default_pip_executable = f"pip{'.exe' if WINDOWS else ''}" + major_pip_executable = f"pip{sys.version_info[0]}{'.exe' if WINDOWS else ''}" + minor_pip_executable = ( + f"pip{sys.version_info[0]}.{sys.version_info[1]}{'.exe' if WINDOWS else ''}" ) expected_pip_executable = minor_pip_executable if ( diff --git a/tests/utils/test_exporter.py b/tests/utils/test_exporter.py index 9e84f10fb12..1a0583e7861 100644 --- a/tests/utils/test_exporter.py +++ b/tests/utils/test_exporter.py @@ -1125,11 +1125,9 @@ def test_exporter_can_export_requirements_txt_with_directory_packages( with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: content = f.read() - expected = """\ -foo @ {}/tests/fixtures/sample_project -""".format( - working_directory.as_uri() - ) + expected = f"""\ +foo @ {working_directory.as_uri()}/tests/fixtures/sample_project +""" assert expected == content @@ -1193,15 +1191,11 @@ def test_exporter_can_export_requirements_txt_with_nested_directory_packages( with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: content = f.read() - expected = """\ -bar @ {}/tests/fixtures/project_with_nested_local/bar -baz @ {}/tests/fixtures/project_with_nested_local -foo @ {}/tests/fixtures/sample_project -""".format( - working_directory.as_uri(), - working_directory.as_uri(), - working_directory.as_uri(), - ) + expected = f"""\ +bar @ {working_directory.as_uri()}/tests/fixtures/project_with_nested_local/bar +baz @ {working_directory.as_uri()}/tests/fixtures/project_with_nested_local +foo @ {working_directory.as_uri()}/tests/fixtures/sample_project +""" assert expected == content @@ -1242,11 +1236,9 @@ def test_exporter_can_export_requirements_txt_with_directory_packages_and_marker with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: content = f.read() - expected = """\ -foo @ {}/tests/fixtures/sample_project ; python_version < "3.7" -""".format( - working_directory.as_uri() - ) + expected = f"""\ +foo @ {working_directory.as_uri()}/tests/fixtures/sample_project ; python_version < "3.7" +""" assert expected == content @@ -1286,11 +1278,9 @@ def test_exporter_can_export_requirements_txt_with_file_packages( with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: content = f.read() - expected = """\ -foo @ {}/tests/fixtures/distributions/demo-0.1.0.tar.gz -""".format( - working_directory.as_uri() - ) + expected = f"""\ +foo @ {working_directory.as_uri()}/tests/fixtures/distributions/demo-0.1.0.tar.gz +""" assert expected == content @@ -1331,11 +1321,9 @@ def test_exporter_can_export_requirements_txt_with_file_packages_and_markers( with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: content = f.read() - expected = """\ -foo @ {}/tests/fixtures/distributions/demo-0.1.0.tar.gz ; python_version < "3.7" -""".format( - working_directory.as_uri() - ) + expected = f"""\ +foo @ {working_directory.as_uri()}/tests/fixtures/distributions/demo-0.1.0.tar.gz ; python_version < "3.7" +""" assert expected == content @@ -1570,7 +1558,7 @@ def test_exporter_exports_requirements_txt_with_dev_extras( with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: content = f.read() - assert content == "{}\n".format("\n".join(expected)) + assert content == "\n".join(expected) + "\n" def test_exporter_exports_requirements_txt_with_legacy_packages_and_duplicate_sources(