Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(python): Support overriding default dev dependencies and mypy config in the python generator #5062

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions generators/python/sdk/versions.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
# For unreleased changes, use unreleased.yml
- version: 4.3.4
irVersion: 53
changelogEntry:
- type: feat
summary: |
You can now override the default dev dependencies that fern generates out of the box using the `extra_dev_dependencies` configuration flag.
For example, to add a specific version of `mypy` as a dev dependency, you can now do the following:

```yaml
config:
extra_dev_dependencies:
mypy: "^1.11.0"
```

Additionally, if fern detects pre-existing mypy configuration in your `pyproject_toml` configuration, it will not add its default configuration for `mypy`.

- version: 4.3.3
irVersion: 53
changelogEntry:
Expand Down
42 changes: 31 additions & 11 deletions generators/python/src/fern_python/codegen/pyproject_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,32 @@ def __init__(
self._user_defined_toml = user_defined_toml

def write(self) -> None:
dev_dependencies = self._dependency_manager.get_dev_dependencies()
default_dev_dependencies = [
Dependency(name="mypy", version="1.0.1"),
Dependency(name="pytest", version="^7.4.0"),
Dependency(name="pytest-asyncio", version="^0.23.5"),
Dependency(name="python-dateutil", version="^2.9.0"),
Dependency(name="types-python-dateutil", version="^2.9.0.20240316"),
]

# Support overriding default dev dependencies
dev_dependency_names = {dep.name for dep in dev_dependencies}
for dep in default_dev_dependencies:
if dep.name not in dev_dependency_names:
dev_dependencies.add(dep)

is_mypy_already_configured = self._user_defined_toml is not None and "[tool.mypy]" in self._user_defined_toml
blocks: List[PyProjectToml.Block] = [
self._poetry_block,
PyProjectToml.DependenciesBlock(
dependencies=self._dependency_manager.get_dependencies(),
dev_dependencies=self._dependency_manager.get_dev_dependencies(),
dev_dependencies=dev_dependencies,
python_version=self._python_version,
),
PyProjectToml.PluginConfigurationBlock(),
PyProjectToml.PluginConfigurationBlock(
is_mypy_already_configured=is_mypy_already_configured,
),
PyProjectToml.BuildSystemBlock(),
]
content = ""
Expand Down Expand Up @@ -225,24 +243,26 @@ def to_string(self) -> str:
python = "{self.python_version}"
{deps}
[tool.poetry.dev-dependencies]
mypy = "1.0.1"
pytest = "^7.4.0"
pytest-asyncio = "^0.23.5"
python-dateutil = "^2.9.0"
types-python-dateutil = "^2.9.0.20240316"
{dev_deps}"""

@dataclass(frozen=True)
class PluginConfigurationBlock(Block):
is_mypy_already_configured: bool

def to_string(self) -> str:
return """
mypy_section = ""
if not self.is_mypy_already_configured:
mypy_section = f"""\
[tool.mypy]
plugins = ["pydantic.mypy"]
"""

return f"""
[tool.pytest.ini_options]
testpaths = [ "tests" ]
asyncio_mode = "auto"

[tool.mypy]
plugins = ["pydantic.mypy"]

{mypy_section}
[tool.ruff]
line-length = 120

Expand Down
Loading