Search code examples
pythondependenciespython-dataclassespython-poetry

how to add dataclasses requirement in poetry for python 3.9


Trying out poetry here for maintaining dependencies. I cant seem to get dataclasses into it.

here is my toml file:

[tool.poetry.dependencies]
python = "^3.9"
certifi = "^2020.12.5"
chardet = "^4.0.0"
hivemind-util = "^0.1b35"
idna = "^2.9"
requests = "^2.25.1"
urllib3 = "^1.26.3"
pytest = "^6.2.2"
pandas = "^1.2.3"
pyarrow = "^3.0.0"
numpy = "^1.20.1"
pyodbc = "^4.0.30"
responses = "^0.12.1"
openpyxl = "^3.0.6"
SQLAlchemy = "^1.3.23"
dataclasses-json = "^0.5.2"

when i run

poetry add dataclasses

I get

 SolverProblemError

  The current project's Python requirement (>=2.7,<2.8 || >=3.4) is not compatible with some of the required packages Python requirement:
    - dataclasses requires Python >=3.6, <3.7, so it will not be satisfied for Python >=2.7,<2.8 || >=3.4,<3.6 || >=3.7

  Because no versions of dataclasses match >0.8,<0.9
   and dataclasses (0.8) requires Python >=3.6, <3.7, dataclasses is forbidden.
  So, because my-project depends on dataclasses (^0.8), version solving failed.

  at ~\.poetry\lib\poetry\puzzle\solver.py:241 in _solve
      237│             packages = result.packages
      238│         except OverrideNeeded as e:
      239│             return self.solve_in_compatibility_mode(e.overrides, use_latest=use_latest)
      240│         except SolveFailure as e:
    → 241│             raise SolverProblemError(e)
      242│
      243│         results = dict(
      244│             depth_first_search(
      245│                 PackageNode(self._package, packages), aggregate_package_nodes

  • Check your dependencies Python requirement: The Python requirement can be specified via the `python` or `markers` properties

    For dataclasses, a possible solution would be to set the `python` property to ">=3.6,<3.7"

    https://python-poetry.org/docs/dependency-specification/#python-restricted-dependencies,
    https://python-poetry.org/docs/dependency-specification/#using-environment-markers

What is wrong here? Dataclasses are avialable for python 3.9


Solution

  • The dataclasses module is part of the standard library of python3.7 and later. You only need to install it if you want to support python3.6 too (See also the description on PyPI: "A backport of the dataclasses module for Python 3.6"). The error message tells you that, too, but in a way that is a little hard to understand if you're not a computer.

    Given that your python version seems to be 3.9, you don't need to do anything - you can just use dataclasses.