Skip to content

Commit

Permalink
Fix encoding warnings with PEP 597 enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
danyeaw committed Jan 21, 2024
1 parent 50a7723 commit d4a1a67
Show file tree
Hide file tree
Showing 20 changed files with 94 additions and 56 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ concurrency:
group: tests-${{ github.head_ref || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

env:
PYTHONWARNDEFAULTENCODING: 'true'

jobs:
tests:
name: ${{ matrix.os }} / ${{ matrix.python-version }}
Expand Down
3 changes: 2 additions & 1 deletion src/poetry/masonry/builders/editable.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ def _add_dist_info(self, added_files: list[Path]) -> None:
json.dumps({
"dir_info": {"editable": True},
"url": self._poetry.file.path.parent.absolute().as_uri(),
})
}),
encoding="utf-8",
)
added_files.append(direct_url_json)

Expand Down
2 changes: 1 addition & 1 deletion src/poetry/repositories/installed_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_package_paths(cls, env: Env, name: str) -> set[Path]:
if not pth_file.exists():
continue

with pth_file.open() as f:
with pth_file.open(encoding="utf-8") as f:
for line in f:
line = line.strip()
if line and not line.startswith(("#", "import ", "import\t")):
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/utils/env/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def _run(self, cmd: list[str], **kwargs: Any) -> str:
output = ""
else:
output = subprocess.check_output(
cmd, stderr=stderr, env=env, text=True, **kwargs
cmd, stderr=stderr, env=env, text=True, encoding="utf-8", **kwargs
)
except CalledProcessError as e:
raise EnvCommandError(e)
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/utils/env/virtual_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def includes_system_site_packages(self) -> bool:
return pyvenv_cfg.exists() and (
re.search(
r"^\s*include-system-site-packages\s*=\s*true\s*$",
pyvenv_cfg.read_text(),
pyvenv_cfg.read_text(encoding="utf-8"),
re.IGNORECASE | re.MULTILINE,
)
is not None
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/utils/setup_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def read_setup_py(self, filepath: Path) -> dict[str, Any]:
def read_setup_cfg(self, filepath: Path) -> dict[str, Any]:
parser = ConfigParser()

parser.read(str(filepath))
parser.read(str(filepath), encoding="utf-8")

name = None
version = None
Expand Down
1 change: 1 addition & 0 deletions src/poetry/vcs/git/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def run(*args: Any, **kwargs: Any) -> None:
stdout=subprocess.DEVNULL,
env=env,
text=True,
encoding="utf-8",
)

@staticmethod
Expand Down
13 changes: 9 additions & 4 deletions tests/console/commands/env/test_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,18 @@ def test_remove_all(
if envs_file == "empty":
envs_file_path.touch()
elif envs_file == "self":
envs_file_path.write_text(f'[{venv_name}]\nminor = "3.9"\npatch = "3.9.1"\n')
envs_file_path.write_text(
f'[{venv_name}]\nminor = "3.9"\npatch = "3.9.1"\n', encoding="utf-8"
)
elif envs_file == "other":
envs_file_path.write_text('[other-abcdefgh]\nminor = "3.9"\npatch = "3.9.1"\n')
envs_file_path.write_text(
'[other-abcdefgh]\nminor = "3.9"\npatch = "3.9.1"\n', encoding="utf-8"
)
elif envs_file == "self_and_other":
envs_file_path.write_text(
f'[{venv_name}]\nminor = "3.9"\npatch = "3.9.1"\n'
'[other-abcdefgh]\nminor = "3.9"\npatch = "3.9.1"\n'
'[other-abcdefgh]\nminor = "3.9"\npatch = "3.9.1"\n',
encoding="utf-8",
)
else:
# no envs file -> nothing to prepare
Expand All @@ -97,7 +102,7 @@ def test_remove_all(

if envs_file is not None:
assert envs_file_path.exists()
envs_file_content = envs_file_path.read_text()
envs_file_content = envs_file_path.read_text(encoding="utf-8")
assert venv_name not in envs_file_content
if "other" in envs_file:
assert "other-abcdefgh" in envs_file_content
Expand Down
12 changes: 8 additions & 4 deletions tests/console/commands/self/test_self_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ def test_generate_system_pyproject_trailing_newline(
example_system_pyproject: str,
) -> None:
cmd = SelfCommand()
cmd.system_pyproject.write_text(example_system_pyproject + "\n" * existing_newlines)
cmd.system_pyproject.write_text(
example_system_pyproject + "\n" * existing_newlines, encoding="utf-8"
)
cmd.generate_system_pyproject()
generated = cmd.system_pyproject.read_text()
generated = cmd.system_pyproject.read_text(encoding="utf-8")

assert len(generated) - len(generated.rstrip("\n")) == existing_newlines

Expand All @@ -40,10 +42,12 @@ def test_generate_system_pyproject_carriage_returns(
example_system_pyproject: str,
) -> None:
cmd = SelfCommand()
cmd.system_pyproject.write_text(example_system_pyproject + "\n")
cmd.system_pyproject.write_text(example_system_pyproject + "\n", encoding="utf-8")
cmd.generate_system_pyproject()

with open(cmd.system_pyproject, newline="") as f: # do not translate newlines
with open(
cmd.system_pyproject, newline="", encoding="utf-8"
) as f: # do not translate newlines
generated = f.read()

assert "\r\r" not in generated
24 changes: 14 additions & 10 deletions tests/console/commands/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_noninteractive(
assert tester.io.fetch_output() == expected
assert tester.io.fetch_error() == ""

toml_content = (tmp_path / "pyproject.toml").read_text()
toml_content = (tmp_path / "pyproject.toml").read_text(encoding="utf-8")
assert 'name = "my-package"' in toml_content
assert 'pytest = "^3.6.0"' in toml_content

Expand Down Expand Up @@ -884,9 +884,11 @@ def test_init_existing_pyproject_simple(
[tool.black]
line-length = 88
"""
pyproject_file.write_text(existing_section)
pyproject_file.write_text(existing_section, encoding="utf-8")
tester.execute(inputs=init_basic_inputs)
assert f"{existing_section}\n{init_basic_toml}" in pyproject_file.read_text()
assert f"{existing_section}\n{init_basic_toml}" in pyproject_file.read_text(
encoding="utf-8"
)


@pytest.mark.parametrize("linesep", ["\n", "\r\n"])
Expand All @@ -902,10 +904,10 @@ def test_init_existing_pyproject_consistent_linesep(
[tool.black]
line-length = 88
""".replace("\n", linesep)
with open(pyproject_file, "w", newline="") as f:
with open(pyproject_file, "w", newline="", encoding="utf-8") as f:
f.write(existing_section)
tester.execute(inputs=init_basic_inputs)
with open(pyproject_file, newline="") as f:
with open(pyproject_file, newline="", encoding="utf-8") as f:
content = f.read()
init_basic_toml = init_basic_toml.replace("\n", linesep)
assert f"{existing_section}{linesep}{init_basic_toml}" in content
Expand All @@ -922,7 +924,7 @@ def test_init_non_interactive_existing_pyproject_add_dependency(
[tool.black]
line-length = 88
"""
pyproject_file.write_text(existing_section)
pyproject_file.write_text(existing_section, encoding="utf-8")

repo.add_package(get_package("foo", "1.19.2"))

Expand All @@ -946,7 +948,9 @@ def test_init_non_interactive_existing_pyproject_add_dependency(
python = "^3.6"
foo = "^1.19.2"
"""
assert f"{existing_section}\n{expected}" in pyproject_file.read_text()
assert f"{existing_section}\n{expected}" in pyproject_file.read_text(
encoding="utf-8"
)


def test_init_existing_pyproject_with_build_system_fails(
Expand All @@ -958,13 +962,13 @@ def test_init_existing_pyproject_with_build_system_fails(
requires = ["setuptools >= 40.6.0", "wheel"]
build-backend = "setuptools.build_meta"
"""
pyproject_file.write_text(existing_section)
pyproject_file.write_text(existing_section, encoding="utf-8")
tester.execute(inputs=init_basic_inputs)
assert (
tester.io.fetch_error().strip()
== "A pyproject.toml file with a defined build-system already exists."
)
assert existing_section in pyproject_file.read_text()
assert existing_section in pyproject_file.read_text(encoding="utf-8")


@pytest.mark.parametrize(
Expand Down Expand Up @@ -1100,7 +1104,7 @@ def mock_check_output(cmd: str, *_: Any, **__: Any) -> str:
python = "^{python}"
"""

assert expected in pyproject_file.read_text()
assert expected in pyproject_file.read_text(encoding="utf-8")


def test_get_pool(mocker: MockerFixture, source_dir: Path) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/console/commands/test_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,4 @@ def mock_check_output(cmd: str, *_: Any, **__: Any) -> str:
python = "^{python}"
"""

assert expected in pyproject_file.read_text()
assert expected in pyproject_file.read_text(encoding="utf-8")
4 changes: 2 additions & 2 deletions tests/console/commands/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ def test_phase_version_update(tester: CommandTester) -> None:

def test_dry_run(tester: CommandTester) -> None:
assert isinstance(tester.command, VersionCommand)
old_pyproject = tester.command.poetry.file.path.read_text()
old_pyproject = tester.command.poetry.file.path.read_text(encoding="utf-8")
tester.execute("--dry-run major")

new_pyproject = tester.command.poetry.file.path.read_text()
new_pyproject = tester.command.poetry.file.path.read_text(encoding="utf-8")
assert tester.io.fetch_output() == "Bumping version from 1.2.3 to 2.0.0\n"
assert old_pyproject == new_pyproject
29 changes: 20 additions & 9 deletions tests/inspection/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def demo_setup(source_dir: Path) -> Path:
"from setuptools import setup; "
'setup(name="demo", '
'version="0.1.0", '
'install_requires=["package"])'
'install_requires=["package"])',
encoding="utf-8",
)
return source_dir

Expand All @@ -76,7 +77,8 @@ def demo_setup_cfg(source_dir: Path) -> Path:
"version = 0.1.0",
"[options]",
"install_requires = package",
])
]),
encoding="utf-8",
)
return source_dir

Expand All @@ -88,15 +90,18 @@ def demo_setup_complex(source_dir: Path) -> Path:
"from setuptools import setup; "
'setup(name="demo", '
'version="0.1.0", '
'install_requires=[i for i in ["package"]])'
'install_requires=[i for i in ["package"]])',
encoding="utf-8",
)
return source_dir


@pytest.fixture
def demo_setup_complex_pep517_legacy(demo_setup_complex: Path) -> Path:
pyproject_toml = demo_setup_complex / "pyproject.toml"
pyproject_toml.write_text('[build-system]\nrequires = ["setuptools", "wheel"]')
pyproject_toml.write_text(
'[build-system]\nrequires = ["setuptools", "wheel"]', encoding="utf-8"
)
return demo_setup_complex


Expand All @@ -109,20 +114,26 @@ def demo_setup_complex_calls_script(
shutil.copytree(fixture_dir("scripts"), scripts_dir)

pyproject = source_dir / "pyproject.toml"
pyproject.write_text(f"""\
pyproject.write_text(
f"""\
[build-system]
requires = ["setuptools", "scripts @ {scripts_dir.as_uri()}"]
build-backend = "setuptools.build_meta:__legacy__"
""")
""",
encoding="utf-8",
)

setup_py = source_dir / "setup.py"
setup_py.write_text("""\
setup_py.write_text(
"""\
import subprocess
from setuptools import setup
if subprocess.call(["exit-code"]) != 42:
raise RuntimeError("Wrong exit code.")
setup(name="demo", version="0.1.0", install_requires=[i for i in ["package"]])
""")
""",
encoding="utf-8",
)

return source_dir

Expand Down Expand Up @@ -327,7 +338,7 @@ def test_info_setup_missing_mandatory_should_trigger_pep517(
setup += ")"

setup_py = source_dir / "setup.py"
setup_py.write_text(setup)
setup_py.write_text(setup, encoding="utf-8")

spy = mocker.spy(VirtualEnv, "run")
_ = PackageInfo.from_directory(source_dir)
Expand Down
2 changes: 1 addition & 1 deletion tests/installation/test_wheel_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_default_installation_dist_info_dir_content(default_installation: Path)

def test_installer_file_contains_valid_version(default_installation: Path) -> None:
installer_file = default_installation / "demo-0.1.0.dist-info" / "INSTALLER"
with open(installer_file) as f:
with open(installer_file, encoding="utf-8") as f:
installer_content = f.read()
match = re.match(r"Poetry (?P<version>.*)", installer_content)
assert match
Expand Down
16 changes: 9 additions & 7 deletions tests/masonry/builders/test_editable_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ def test_builder_installs_proper_files_for_standard_packages(
assert tmp_venv.site_packages.exists(pth_file)
assert (
simple_poetry.file.path.parent.resolve().as_posix()
== tmp_venv.site_packages.find(pth_file)[0].read_text().strip(os.linesep)
== tmp_venv.site_packages.find(pth_file)[0]
.read_text(encoding="utf-8")
.strip(os.linesep)
)

dist_info = Path("simple_project-1.2.3.dist-info")
Expand All @@ -117,12 +119,12 @@ def test_builder_installs_proper_files_for_standard_packages(
"dir_info": {"editable": True},
"url": simple_poetry.file.path.parent.as_uri(),
},
json.loads(dist_info.joinpath("direct_url.json").read_text()),
json.loads(dist_info.joinpath("direct_url.json").read_text(encoding="utf-8")),
)

assert dist_info.joinpath("INSTALLER").read_text() == "poetry"
assert dist_info.joinpath("INSTALLER").read_text(encoding="utf-8") == "poetry"
assert (
dist_info.joinpath("entry_points.txt").read_text()
dist_info.joinpath("entry_points.txt").read_text(encoding="utf-8")
== "[console_scripts]\nbaz=bar:baz.boom.bim\nfoo=foo:bar\n"
"fox=fuz.foo:bar.baz\n\n"
)
Expand Down Expand Up @@ -184,7 +186,7 @@ def test_builder_installs_proper_files_for_standard_packages(
sys.exit(baz.boom.bim())
"""

assert baz_script == tmp_venv._bin_dir.joinpath("baz").read_text()
assert baz_script == tmp_venv._bin_dir.joinpath("baz").read_text(encoding="utf-8")

foo_script = f"""\
#!{tmp_venv.python}
Expand All @@ -195,7 +197,7 @@ def test_builder_installs_proper_files_for_standard_packages(
sys.exit(bar())
"""

assert foo_script == tmp_venv._bin_dir.joinpath("foo").read_text()
assert foo_script == tmp_venv._bin_dir.joinpath("foo").read_text(encoding="utf-8")

fox_script = f"""\
#!{tmp_venv.python}
Expand All @@ -206,7 +208,7 @@ def test_builder_installs_proper_files_for_standard_packages(
sys.exit(bar.baz())
"""

assert fox_script == tmp_venv._bin_dir.joinpath("fox").read_text()
assert fox_script == tmp_venv._bin_dir.joinpath("fox").read_text(encoding="utf-8")


def test_builder_falls_back_on_setup_and_pip_for_packages_with_build_scripts(
Expand Down
6 changes: 3 additions & 3 deletions tests/pyproject/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@pytest.fixture
def pyproject_toml(tmp_path: Path) -> Path:
path = tmp_path / "pyproject.toml"
with path.open(mode="w"):
with path.open(mode="w", encoding="utf-8"):
pass
return path

Expand All @@ -24,7 +24,7 @@ def build_system_section(pyproject_toml: Path) -> str:
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
"""
with pyproject_toml.open(mode="a") as f:
with pyproject_toml.open(mode="a", encoding="utf-8") as f:
f.write(content)
return content

Expand All @@ -38,6 +38,6 @@ def poetry_section(pyproject_toml: Path) -> str:
[tool.poetry.dependencies]
python = "^3.5"
"""
with pyproject_toml.open(mode="a") as f:
with pyproject_toml.open(mode="a", encoding="utf-8") as f:
f.write(content)
return content
Loading

0 comments on commit d4a1a67

Please sign in to comment.