Search code examples
pythonpython-poetry

How to use python black formatter under a project for python>=3.5 managed by poetry?


I created a python project "foo" with Poetry. This is the content of pyproject.toml:

[tool.poetry]
name = "bar"
version = "0.1.0"
description = ""

[tool.poetry.dependencies]
python = ">=3.5"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

This package is compatible with Python3.5. I want to black formatter, which is not compatible with Python3.5. I think there is no problem if I use Python>=3.6 for development, but I cannot install black formatter:

$ poetry add black --dev
[SolverProblemError]
The current project's Python requirement (>=3.5) is not compatible with some of the required packages Python requirement:
  - black requires Python >=3.6

Because no versions of black match >19.10b0,<20.0
 and black (19.10b0) requires Python >=3.6, black is forbidden.
So, because bar depends on black (^19.10b0), version solving failed.

So I installed black directly with pip:

$ poetry run pip install black

This way doesn't sit well with me. I want to install black by poetry.

How should I do? (I don't want to modify the dependency to python>=3.6)


Solution

  • Seems a bit late but actually you can do what you want even if black supports only Python >=3.6.2

    In your pyproject.toml you can define a restricted dependcy as documented in https://python-poetry.org/docs/dependency-specification/#python-restricted-dependencies

    [tool.poetry.dependencies]
    python = ">=3.5"
    
    [tool.poetry.dev-dependencies]
    black = {version = "^21.7b0", python = ">=3.6.2"}
    

    Poetry won't complain and you won't have any problems since it is a dev dependency.