1919from poetry .core .constraints .generic import Constraint
2020from poetry .core .constraints .generic import MultiConstraint
2121from poetry .core .constraints .generic import UnionConstraint
22+ from poetry .core .constraints .generic .parser import STR_CMP_CONSTRAINT
2223from poetry .core .constraints .version import VersionConstraint
2324from poetry .core .constraints .version import VersionUnion
2425from poetry .core .constraints .version .exceptions import ParseConstraintError
@@ -336,7 +337,11 @@ def __hash__(self) -> int:
336337
337338
338339class SingleMarker (SingleMarkerLike [Union [BaseConstraint , VersionConstraint ]]):
339- _CONSTRAINT_RE = re .compile (r"(?i)^(~=|!=|>=?|<=?|==?=?|in|not in)?\s*(.+)$" )
340+ _CONSTRAINT_RE_PATTERN_1 = re .compile (
341+ r"(?i)^(?P<op>~=|!=|>=?|<=?|==?=?|not in|in)?\s*(?P<value>.+)$"
342+ )
343+ _CONSTRAINT_RE_PATTERN_2 = STR_CMP_CONSTRAINT
344+
340345 VALUE_SEPARATOR_RE = re .compile ("[ ,|]+" )
341346 _VERSION_LIKE_MARKER_NAME : ClassVar [set [str ]] = {
342347 "python_version" ,
@@ -345,7 +350,10 @@ class SingleMarker(SingleMarkerLike[Union[BaseConstraint, VersionConstraint]]):
345350 }
346351
347352 def __init__ (
348- self , name : str , constraint : str | BaseConstraint | VersionConstraint
353+ self ,
354+ name : str ,
355+ constraint : str | BaseConstraint | VersionConstraint ,
356+ swapped_name_value : bool = False ,
349357 ) -> None :
350358 from poetry .core .constraints .generic import (
351359 parse_constraint as parse_generic_constraint ,
@@ -355,20 +363,29 @@ def __init__(
355363 parsed_constraint : BaseConstraint | VersionConstraint
356364 parser : Callable [[str ], BaseConstraint | VersionConstraint ]
357365 original_constraint_string = constraint_string = str (constraint )
366+ self ._swapped_name_value : bool = swapped_name_value
367+
368+ if swapped_name_value :
369+ pattern = self ._CONSTRAINT_RE_PATTERN_2
370+ else :
371+ pattern = self ._CONSTRAINT_RE_PATTERN_1
358372
359- # Extract operator and value
360- m = self ._CONSTRAINT_RE .match (constraint_string )
373+ m = pattern .match (constraint_string )
361374 if m is None :
362375 raise InvalidMarker (f"Invalid marker for '{ name } ': { constraint_string } " )
363376
364- self ._operator = m .group (1 )
377+ self ._operator = m .group ("op" )
365378 if self ._operator is None :
366379 self ._operator = "=="
367380
368- self ._value = m .group (2 )
381+ self ._value = m .group ("value" )
369382 parser = parse_generic_constraint
370383
371- if name in self ._VERSION_LIKE_MARKER_NAME :
384+ if swapped_name_value and name not in PYTHON_VERSION_MARKERS :
385+ # Something like `"tegra" in platform_release`
386+ # or `"arm" not in platform_version`.
387+ pass
388+ elif name in self ._VERSION_LIKE_MARKER_NAME :
372389 parser = parse_marker_version_constraint
373390
374391 if self ._operator in {"in" , "not in" }:
@@ -472,7 +489,11 @@ def invert(self) -> BaseMarker:
472489 # We should never go there
473490 raise RuntimeError (f"Invalid marker operator '{ self ._operator } '" )
474491
475- return parse_marker (f"{ self ._name } { operator } '{ self ._value } '" )
492+ if self ._swapped_name_value :
493+ constraint = f'"{ self ._value } " { operator } { self ._name } '
494+ else :
495+ constraint = f'{ self ._name } { operator } "{ self ._value } "'
496+ return parse_marker (constraint )
476497
477498 def __eq__ (self , other : object ) -> bool :
478499 if not isinstance (other , SingleMarker ):
@@ -484,6 +505,8 @@ def __hash__(self) -> int:
484505 return hash (self ._key )
485506
486507 def __str__ (self ) -> str :
508+ if self ._swapped_name_value :
509+ return f'"{ self ._value } " { self ._operator } { self ._name } '
487510 return f'{ self ._name } { self ._operator } "{ self ._value } "'
488511
489512
@@ -961,11 +984,21 @@ def _compact_markers(
961984
962985 elif token .data == f"{ tree_prefix } item" :
963986 name , op , value = token .children
964- if value .type == f"{ tree_prefix } MARKER_NAME" :
987+ swapped_name_value = value .type == f"{ tree_prefix } MARKER_NAME"
988+ stringed_value = name .type in {
989+ f"{ tree_prefix } ESCAPED_STRING" ,
990+ f"{ tree_prefix } SINGLE_QUOTED_STRING" ,
991+ }
992+ if swapped_name_value :
965993 name , value = value , name
966994
967995 value = value [1 :- 1 ]
968- sub_marker = SingleMarker (str (name ), f"{ op } { value } " )
996+
997+ sub_marker = SingleMarker (
998+ str (name ),
999+ f'"{ value } " { op } ' if stringed_value else f"{ op } { value } " ,
1000+ swapped_name_value = swapped_name_value ,
1001+ )
9691002 groups [- 1 ].append (sub_marker )
9701003
9711004 elif token .data == f"{ tree_prefix } BOOL_OP" and token .children [0 ] == "or" :
0 commit comments