Skip to content

Commit

Permalink
chore(toml): remove type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
branchvincent committed May 4, 2022
1 parent 722d439 commit dd50924
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 14 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ repos:
hooks:
- id: mypy
pass_filenames: false
additional_dependencies: [tomlkit==0.10.2]
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ module = [
'poetry.core.packages.dependency',
'poetry.core.packages.package',
'poetry.core.semver.version_union',
'poetry.core.toml.exceptions',
'poetry.core.toml.file',
]
ignore_errors = true

Expand Down
5 changes: 3 additions & 2 deletions src/poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import List
from typing import Mapping
from typing import Union
from typing import cast
from warnings import warn

from poetry.core.utils.helpers import combine_unicode
Expand Down Expand Up @@ -55,8 +56,8 @@ def create_poetry(
raise RuntimeError("The Poetry configuration is invalid:\n" + message)

# Load package
name = local_config["name"]
version = local_config["version"]
name = cast(str, local_config["name"])
version = cast(str, local_config["version"])
package = self.get_package(name, version)
package = self.configure_package(
package, local_config, poetry_file.parent, with_groups=with_groups
Expand Down
11 changes: 7 additions & 4 deletions src/poetry/core/pyproject/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

from typing import TYPE_CHECKING
from typing import Any
from typing import cast

from tomlkit.container import Container


if TYPE_CHECKING:
from pathlib import Path

from tomlkit.container import Container
from tomlkit.toml_document import TOMLDocument

from poetry.core.pyproject.tables import BuildSystem
Expand Down Expand Up @@ -63,7 +65,7 @@ def poetry_config(self) -> Container:
from tomlkit.exceptions import NonExistentKey

try:
return self.data["tool"]["poetry"]
return cast(Container, self.data["tool"]["poetry"]) # type: ignore[index]
except NonExistentKey as e:
from poetry.core.pyproject.exceptions import PyProjectException

Expand Down Expand Up @@ -95,8 +97,9 @@ def save(self) -> None:
if "build-system" not in data:
data["build-system"] = Container()

data["build-system"]["requires"] = self._build_system.requires
data["build-system"]["build-backend"] = self._build_system.build_backend
build_system = cast(Container, data["build-system"])
build_system["requires"] = self._build_system.requires
build_system["build-backend"] = self._build_system.build_backend

self.file.write(data=data)

Expand Down
2 changes: 1 addition & 1 deletion src/poetry/core/utils/toml_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class TomlFile(TOMLFile):
@classmethod
def __new__(cls: type[TOMLFile], *args: Any, **kwargs: Any) -> TOMLFile:
def __new__(cls: type[TOMLFile], *args: Any, **kwargs: Any) -> TOMLFile: # type: ignore[misc]
import warnings

this_import = f"{cls.__module__}.{cls.__name__}"
Expand Down
4 changes: 3 additions & 1 deletion tests/pyproject/test_pyproject_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import uuid

from pathlib import Path
from typing import Any

import pytest

Expand Down Expand Up @@ -37,7 +38,8 @@ def test_pyproject_toml_poetry_config(
pyproject_toml: Path, poetry_section: str
) -> None:
pyproject = PyProjectTOML(pyproject_toml)
config = TOMLFile(pyproject_toml.as_posix()).read()["tool"]["poetry"]
doc: dict[str, Any] = TOMLFile(pyproject_toml.as_posix()).read()
config = doc["tool"]["poetry"]

assert pyproject.is_poetry_project()
assert pyproject.poetry_config == config
Expand Down
12 changes: 8 additions & 4 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,16 @@ def test_create_poetry_with_multi_constraints_dependency() -> None:

def test_validate() -> None:
complete = TOMLFile(fixtures_dir / "complete.toml")
content = complete.read()["tool"]["poetry"]
doc: dict[str, Any] = complete.read()
content = doc["tool"]["poetry"]

assert Factory.validate(content) == {"errors": [], "warnings": []}


def test_validate_fails() -> None:
complete = TOMLFile(fixtures_dir / "complete.toml")
content = complete.read()["tool"]["poetry"]
doc: dict[str, Any] = complete.read()
content = doc["tool"]["poetry"]
content["this key is not in the schema"] = ""

expected = (
Expand All @@ -194,14 +196,16 @@ def test_validate_fails() -> None:

def test_strict_validation_success_on_multiple_readme_files() -> None:
with_readme_files = TOMLFile(fixtures_dir / "with_readme_files" / "pyproject.toml")
content = with_readme_files.read()["tool"]["poetry"]
doc: dict[str, Any] = with_readme_files.read()
content = doc["tool"]["poetry"]

assert Factory.validate(content, strict=True) == {"errors": [], "warnings": []}


def test_strict_validation_fails_on_readme_files_with_unmatching_types() -> None:
with_readme_files = TOMLFile(fixtures_dir / "with_readme_files" / "pyproject.toml")
content = with_readme_files.read()["tool"]["poetry"]
doc: dict[str, Any] = with_readme_files.read()
content = doc["tool"]["poetry"]
content["readme"][0] = "README.md"

assert Factory.validate(content, strict=True) == {
Expand Down

0 comments on commit dd50924

Please sign in to comment.