1313import re
1414import tomllib
1515from collections .abc import Iterable , Iterator , Mapping
16- from dataclasses import dataclass
16+ from dataclasses import dataclass , replace
1717from pathlib import PurePath , PurePosixPath
1818from types import MappingProxyType
1919from typing import Any , ClassVar
2020
2121from coretrace_python .analysis import Analysis , AnalysisContext
2222from coretrace_python .findings import Severity
2323from coretrace_python .semantic .symbols import SymbolId
24- from coretrace_python .source import SourceFile , SourceId , SourceSpan
24+ from coretrace_python .source import Location , SourceFile , SourceId , SourceSpan , lines_of
25+ from coretrace_python .source .positions import Pointer , TomlPositions , file_location
2526
2627_REQUIREMENT = re .compile (r"^\s*([A-Za-z0-9][A-Za-z0-9._-]*)\s*(\[[^\]]*\])?\s*(.*)$" )
2728_CLAUSE = re .compile (r"^(===|==|!=|<=|>=|~=|<|>|\^)\s*([0-9][0-9A-Za-z.*+!-]*)$" )
@@ -115,11 +116,16 @@ def _lower_bounds(specifier: str) -> list[Version]:
115116
116117@dataclass (frozen = True )
117118class Requirement :
119+ """``span`` is where the requirement is written: a verified line, or, for a locked
120+ package whose entry cannot be placed, a file location. ``locked`` is the version a
121+ lock file pins, as it writes it; None for a declared requirement."""
122+
118123 name : str
119124 specifier : str
120- span : SourceSpan
125+ span : Location
121126 pinned : Version | None = None
122127 optional : bool = False
128+ locked : str | None = None
123129
124130 @classmethod
125131 def parse (cls , text : str , source_id : SourceId , line : int , optional : bool = False ) -> Requirement | None :
@@ -313,12 +319,14 @@ def merge(self, other: DependencyGraph) -> DependencyGraph:
313319 if current is None :
314320 merged [name ] = requirement
315321 continue
322+ declared = current if current .specifier else requirement
316323 merged [name ] = Requirement (
317324 name ,
318325 current .specifier or requirement .specifier ,
319- current . span if current . specifier else requirement .span ,
326+ declared .span ,
320327 requirement .pinned or current .pinned ,
321328 current .optional and requirement .optional ,
329+ declared .locked ,
322330 )
323331 dependents = dict (self ._dependents )
324332 for name , found in other ._dependents .items ():
@@ -341,7 +349,7 @@ def parse_dependencies(source: SourceFile) -> DependencyGraph:
341349
342350def _parse_requirements_txt (source : SourceFile ) -> DependencyGraph :
343351 found : dict [str , Requirement ] = {}
344- for number , line in enumerate (source .text . splitlines ( ), start = 1 ):
352+ for number , line in enumerate (lines_of ( source .text ), start = 1 ):
345353 stripped = line .strip ()
346354 if not stripped or stripped .startswith (("#" , "-" )):
347355 continue
@@ -362,25 +370,32 @@ def _parse_toml(source: SourceFile, extract: Any, dependents: Any = None) -> Dep
362370 return DependencyGraph (found , dependents = None if dependents is None else dependents (data ))
363371
364372
365- def _line_of (source : SourceFile , key : str , default : int = 1 ) -> int :
366- for number , line in enumerate (source .text .splitlines (), start = 1 ):
367- if key in line :
368- return number
369- return default
373+ def _pyproject_requirements (data : Mapping [str , Any ], source : SourceFile ) -> list [Requirement ]:
374+ """The requirements ``pyproject.toml`` declares (PEP 621 and Poetry), each located at
375+ its own array element or key, which the position component checks, or given a file
376+ location when it cannot be placed."""
370377
378+ positions = TomlPositions (source .text )
371379
372- def _pyproject_requirements (data : Mapping [str , Any ], source : SourceFile ) -> list [Requirement ]:
373- found : list [Requirement ] = []
380+ def declared (text : str , path : Pointer , value : object , optional : bool = False ) -> Requirement | None :
381+ requirement = Requirement .parse (text , source .source_id , 1 , optional )
382+ if requirement is None :
383+ return None
384+ place = positions .locate (path , value )
385+ location : Location = (
386+ SourceSpan (source .source_id , * place )
387+ if place is not None
388+ else file_location (source .source_id , path , text )
389+ )
390+ return replace (requirement , span = location )
391+
392+ found : list [Requirement | None ] = []
374393 project = data .get ("project" , {})
375- for text in project .get ("dependencies" , []) or []:
376- requirement = Requirement .parse (text , source .source_id , _line_of (source , text ))
377- if requirement is not None :
378- found .append (requirement )
379- for group in (project .get ("optional-dependencies" , {}) or {}).values ():
380- for text in group or []:
381- requirement = Requirement .parse (text , source .source_id , _line_of (source , text ), True )
382- if requirement is not None :
383- found .append (requirement )
394+ for index , text in enumerate (project .get ("dependencies" , []) or []):
395+ found .append (declared (text , ("project" , "dependencies" , index ), text ))
396+ for group , texts in (project .get ("optional-dependencies" , {}) or {}).items ():
397+ for index , text in enumerate (texts or []):
398+ found .append (declared (text , ("project" , "optional-dependencies" , group , index ), text , True ))
384399 poetry = data .get ("tool" , {}).get ("poetry" , {})
385400 for section , optional in (("dependencies" , False ), ("dev-dependencies" , True )):
386401 for name , spec in (poetry .get (section , {}) or {}).items ():
@@ -389,23 +404,29 @@ def _pyproject_requirements(data: Mapping[str, Any], source: SourceFile) -> list
389404 specifier = spec .get ("version" , "" ) if isinstance (spec , dict ) else str (spec )
390405 if specifier == "*" :
391406 specifier = ""
392- requirement = Requirement .parse (
393- f"{ name } { specifier } " , source .source_id , _line_of (source , name ), optional
394- )
395- if requirement is not None :
396- found .append (requirement )
397- return found
407+ found .append (declared (f"{ name } { specifier } " , ("tool" , "poetry" , section , name ), spec , optional ))
408+ return [requirement for requirement in found if requirement is not None ]
398409
399410
400411def _lock_requirements (data : Mapping [str , Any ], source : SourceFile ) -> list [Requirement ]:
412+ """The packages a lock file pins, each located at the ``name`` of its own
413+ ``[[package]]`` entry, which the position component checks."""
414+
401415 found : list [Requirement ] = []
402- for package in data .get ("package" , []) or []:
416+ positions = TomlPositions (source .text )
417+ for index , package in enumerate (data .get ("package" , []) or []):
403418 name , version = package .get ("name" ), package .get ("version" )
404419 if not isinstance (name , str ) or not isinstance (version , str ):
405420 continue
406- line = _line_of (source , f'name = "{ name } "' )
421+ path = ("package" , index , "name" )
422+ place = positions .locate (path , name )
423+ location : Location = (
424+ SourceSpan (source .source_id , * place )
425+ if place is not None
426+ else file_location (source .source_id , path , name )
427+ )
407428 found .append (
408- Requirement (normalize (name ), "" , SourceSpan ( source . source_id , line , 1 ), Version .parse (version ))
429+ Requirement (normalize (name ), "" , location , Version .parse (version ), locked = version )
409430 )
410431 return found
411432
0 commit comments