File tree Expand file tree Collapse file tree 9 files changed +73
-0
lines changed
Expand file tree Collapse file tree 9 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -843,6 +843,17 @@ any custom url in the `urls` section.
843843
844844If you publish your package on PyPI, they will appear in the ` Project Links ` section.
845845
846+ ## ` requires-poetry `
847+
848+ A constraint for the Poetry version that is required for this project.
849+ If you are using a Poetry version that is not allowed by this constraint,
850+ an error will be raised.
851+
852+ ``` toml
853+ [tool .poetry ]
854+ requires-poetry = " >=2.0"
855+ ```
856+
846857## ` requires-plugins `
847858
848859In this section, you can specify that certain plugins are required for your project:
Original file line number Diff line number Diff line change 1010
1111from cleo .io .null_io import NullIO
1212from packaging .utils import canonicalize_name
13+ from poetry .core .constraints .version import Version
14+ from poetry .core .constraints .version import parse_constraint
1315from poetry .core .factory import Factory as BaseFactory
1416from poetry .core .packages .dependency_group import MAIN_GROUP
1517
18+ from poetry .__version__ import __version__
1619from poetry .config .config import Config
1720from poetry .exceptions import PoetryError
1821from poetry .json import validate_object
@@ -56,6 +59,15 @@ def create_poetry(
5659
5760 base_poetry = super ().create_poetry (cwd = cwd , with_groups = with_groups )
5861
62+ if version_str := base_poetry .local_config .get ("requires-poetry" ):
63+ version_constraint = parse_constraint (version_str )
64+ version = Version .parse (__version__ )
65+ if not version_constraint .allows (version ):
66+ raise PoetryError (
67+ f"This project requires Poetry { version_constraint } ,"
68+ f" but you are using Poetry { version } "
69+ )
70+
5971 poetry_file = base_poetry .pyproject_path
6072 locker = Locker (poetry_file .parent / "poetry.lock" , base_poetry .pyproject .data )
6173
Original file line number Diff line number Diff line change 44 "type" : " object" ,
55 "required" : [],
66 "properties" : {
7+ "requires-poetry" : {
8+ "type" : " string" ,
9+ "description" : " The version constraint for Poetry itself." ,
10+ "$ref" : " #/definitions/dependency"
11+ },
712 "requires-plugins" : {
813 "type" : " object" ,
914 "description" : " Poetry plugins that are required for this project." ,
Original file line number Diff line number Diff line change 1+ [tool .poetry ]
2+ package-mode = false
3+ requires-poetry = " <1.2"
4+
5+ [tool .poetry .dependencies ]
6+ python = " ^3.8"
Original file line number Diff line number Diff line change 1+ [tool .poetry ]
2+ package-mode = false
3+ requires-poetry = " >=1.2"
4+
5+ [tool .poetry .dependencies ]
6+ python = " ^3.8"
Original file line number Diff line number Diff line change 1+ [tool .poetry ]
2+ name = " foobar"
3+ version = " 0.1.0"
4+ description = " "
5+ authors = [" Your Name <you@example.com>" ]
6+ requires-poetry = 2
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ name = "foobar"
33version = " 0.1.0"
44description = " "
55authors = [" Your Name <you@example.com>" ]
6+ requires-poetry = " >=2.0"
67
78[tool .poetry .requires-plugins ]
89foo = " >=1.0"
Original file line number Diff line number Diff line change @@ -39,6 +39,14 @@ def test_self_valid() -> None:
3939 assert Factory .validate (toml ) == {"errors" : [], "warnings" : []}
4040
4141
42+ def test_self_invalid_version () -> None :
43+ toml : dict [str , Any ] = TOMLFile (FIXTURE_DIR / "self_invalid_version.toml" ).read ()
44+ assert Factory .validate (toml ) == {
45+ "errors" : ["data.requires-poetry must be string" ],
46+ "warnings" : [],
47+ }
48+
49+
4250def test_self_invalid_plugin () -> None :
4351 toml : dict [str , Any ] = TOMLFile (FIXTURE_DIR / "self_invalid_plugin.toml" ).read ()
4452 assert Factory .validate (toml ) == {
Original file line number Diff line number Diff line change 1414from poetry .core .packages .package import Package
1515from poetry .core .packages .vcs_dependency import VCSDependency
1616
17+ from poetry .__version__ import __version__
1718from poetry .exceptions import PoetryError
1819from poetry .factory import Factory
1920from poetry .plugins .plugin import Plugin
@@ -230,6 +231,23 @@ def test_create_poetry_non_package_mode(fixture_dir: FixtureDirGetter) -> None:
230231 assert not poetry .is_package_mode
231232
232233
234+ def test_create_poetry_version_ok (fixture_dir : FixtureDirGetter ) -> None :
235+ io = BufferedIO ()
236+ Factory ().create_poetry (fixture_dir ("self_version_ok" ), io = io )
237+
238+ assert io .fetch_output () == ""
239+ assert io .fetch_error () == ""
240+
241+
242+ def test_create_poetry_version_not_ok (fixture_dir : FixtureDirGetter ) -> None :
243+ with pytest .raises (PoetryError ) as e :
244+ Factory ().create_poetry (fixture_dir ("self_version_not_ok" ))
245+ assert (
246+ str (e .value )
247+ == f"This project requires Poetry <1.2, but you are using Poetry { __version__ } "
248+ )
249+
250+
233251@pytest .mark .parametrize (
234252 "project" ,
235253 ("with_primary_source_implicit" , "with_primary_source_explicit" ),
You can’t perform that action at this time.
0 commit comments