dependabot-python 0.404.0 → 0.405.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 658f2f5ddb3748a69e82dc4f8bf49d767a2c0dae9cbb8914a593d20ef5c07ea3
4
- data.tar.gz: 68b2055158a4c722df9189946fbf1a187fcf30772d891c34656ee528d77f5301
3
+ metadata.gz: 2c686baaf28bbd6b011f5cf893be556e402d2d89fb3be2fb94215fc111c397b4
4
+ data.tar.gz: a681902018861b463591b7a46270d5718c0617f2fde9585d711f52bfcbca1bda
5
5
  SHA512:
6
- metadata.gz: 94b57c466aca413a45ac4cf7d467c3e6267e44e045a7d60d5c81d938f3ab272022b01cf678842350cc9ec22e6292b77698d562fc368061302581f3a60dd80eed
7
- data.tar.gz: 804c699ef9572ecba46b5de36b11d25fdd06f869e514f411f3e82cf109ff829855a38ec333d523ee49d1dfa9e8e1270d8d50eb147f1839e86ad6dd54808be4c7
6
+ metadata.gz: 7321f79551f821b967ae706829f3b8d8595ccc48ccc5b3ca0f43bd356623771b0f3fd051091230f108132ad68bdfa364fc703d9219dab762bb62f4612736bf3b
7
+ data.tar.gz: 0afa4dd039b8e32b8cc762b09884b07ff8a0544bebdcc24f976d30015521b49e088e0c47abf388a03b80cf5dadef339b0a6d30ddec8fc0c8fa79acc583b271d2
@@ -171,20 +171,24 @@ def parse_pep621_pep735_dependencies(pyproject_path):
171
171
  return json.dumps({"result": dependencies})
172
172
 
173
173
 
174
- def parse_requirements(directory):
175
- # Parse the requirements.txt
174
+ def parse_requirements(directory, root_files=None):
175
+ # Without root_files, every requirements file on disk is parsed as a root.
176
+ # With them, the remaining files are only read through `-r`/`-c` lines,
177
+ # as pip would.
176
178
  requirement_packages = []
177
- requirement_files = glob.glob(os.path.join(directory, '*.txt')) \
178
- + glob.glob(os.path.join(directory, '**', '*.txt'))
179
-
180
- pip_compile_files = glob.glob(os.path.join(directory, '*.in')) \
181
- + glob.glob(os.path.join(directory, '**', '*.in'))
179
+ if root_files is None:
180
+ reqs_files = glob.glob(os.path.join(directory, '*.txt')) \
181
+ + glob.glob(os.path.join(directory, '**', '*.txt')) \
182
+ + glob.glob(os.path.join(directory, '*.in')) \
183
+ + glob.glob(os.path.join(directory, '**', '*.in'))
184
+ else:
185
+ reqs_files = [os.path.join(directory, f) for f in root_files]
182
186
 
183
187
  def version_from_install_req(install_req):
184
188
  if install_req.is_pinned:
185
189
  return next(iter(install_req.specifier)).version
186
190
 
187
- for reqs_file in requirement_files + pip_compile_files:
191
+ for reqs_file in reqs_files:
188
192
  try:
189
193
  requirements = pip._internal.req.req_file.parse_requirements(
190
194
  reqs_file,
@@ -214,7 +218,8 @@ def parse_requirements(directory):
214
218
  "markers": str(install_req.markers) or None,
215
219
  "file": rel_path,
216
220
  "requirement": str(install_req.specifier) or None,
217
- "extras": sorted(list(install_req.extras))
221
+ "extras": sorted(list(install_req.extras)),
222
+ "constraint": parsed_req.constraint
218
223
  })
219
224
  except Exception as e:
220
225
  print(json.dumps({"error": repr(e)}))
data/helpers/run.py CHANGED
@@ -9,7 +9,7 @@ if __name__ == "__main__":
9
9
  # TODO Python 3.10 added native switch statements, so switch this if/elif
10
10
  # to that once we drop support for 3.9.
11
11
  if args["function"] == "parse_requirements":
12
- print(parser.parse_requirements(args["args"][0]))
12
+ print(parser.parse_requirements(*args["args"]))
13
13
  elif args["function"] == "parse_setup":
14
14
  print(parser.parse_setup(args["args"][0]))
15
15
  elif args["function"] == "parse_pep621_pep735_dependencies":
@@ -17,6 +17,11 @@ def parse(fixture_dir):
17
17
  return result
18
18
 
19
19
 
20
+ def parse_roots(fixture_dir, root_files):
21
+ path = os.path.join(FIXTURES, fixture_dir)
22
+ return json.loads(parse_requirements(path, root_files))["result"]
23
+
24
+
20
25
  def find_dep(deps, name):
21
26
  return next((d for d in deps if d["name"] == name), None)
22
27
 
@@ -101,3 +106,15 @@ class TestParseRequirementsTxt:
101
106
  files = {d["file"] for d in deps}
102
107
  assert any("requirements.txt" in f for f in files)
103
108
  assert any("requirements-dev.txt" in f for f in files)
109
+
110
+ def test_flags_requirements_included_via_constraint(self):
111
+ deps = parse_roots("requirements", ["with_constraints.txt"])
112
+ flags = {(d["file"], d["constraint"]) for d in deps}
113
+ assert flags == {
114
+ ("with_constraints.txt", False),
115
+ ("constraints.txt", True),
116
+ }
117
+
118
+ def test_parses_only_the_given_root_files(self):
119
+ deps = parse_roots("requirements", ["requirements-dev.txt"])
120
+ assert {d["file"] for d in deps} == {"requirements-dev.txt"}
@@ -1,12 +1,11 @@
1
1
  # typed: strong
2
2
  # frozen_string_literal: true
3
3
 
4
- require "pathname"
5
4
  require "sorbet-runtime"
6
5
 
7
6
  require "dependabot/dependency_file"
8
7
  require "dependabot/dependency_graphers/base"
9
- require "dependabot/python/shared_file_fetcher"
8
+ require "dependabot/python/requirement_references"
10
9
 
11
10
  module Dependabot
12
11
  module Python
@@ -57,27 +56,6 @@ module Dependabot
57
56
  path.match?(DEPEND_TXT_REGEX)
58
57
  end
59
58
 
60
- # Resolves the sibling paths a requirements file references via `-r`/`-c`, returning repo-relative
61
- # cleanpaths (matching fetched DependencyFile names). Single home for reference resolution, shared by the
62
- # grapher's bystander filter and the layering group builder so reference-syntax handling never drifts.
63
- #
64
- # Reuses SharedFileFetcher's reference regexes so callers keep exactly the children the file fetcher pulled in.
65
- sig { params(file: Dependabot::DependencyFile).returns(T::Array[String]) }
66
- def self.referenced_paths(file)
67
- content = file.content
68
- return [] if content.nil?
69
-
70
- current_dir = File.dirname(file.name)
71
- referenced =
72
- content.scan(Dependabot::Python::SharedFileFetcher::CHILD_REQUIREMENT_REGEX).flatten +
73
- content.scan(Dependabot::Python::SharedFileFetcher::CONSTRAINT_REGEX).flatten
74
-
75
- referenced.map do |path|
76
- resolved = current_dir == "." ? path : File.join(current_dir, path)
77
- Pathname.new(resolved).cleanpath.to_path
78
- end
79
- end
80
-
81
59
  sig { params(dependency_files: T::Array[Dependabot::DependencyFile]).void }
82
60
  def initialize(dependency_files:)
83
61
  @dependency_files = dependency_files
@@ -169,7 +147,7 @@ module Dependabot
169
147
 
170
148
  until queue.empty?
171
149
  current = T.must(queue.shift)
172
- self.class.referenced_paths(current).each do |path|
150
+ RequirementReferences.paths(current).each do |path|
173
151
  next if seen.include?(path)
174
152
 
175
153
  seen << path
@@ -12,6 +12,7 @@ require "dependabot/python/language_version_manager"
12
12
  require "dependabot/python/name_normaliser"
13
13
  require "dependabot/python/pip_compile_file_matcher"
14
14
  require "dependabot/python/pipenv_runner"
15
+ require "dependabot/python/requirement_references"
15
16
  require "dependabot/python/shared_file_fetcher"
16
17
  require "toml-rb"
17
18
 
@@ -63,6 +64,10 @@ module Dependabot
63
64
  generate_ephemeral_lockfile!
64
65
  emit_missing_lockfile_warning! if @ephemeral_lockfile_generated
65
66
  end
67
+
68
+ # Graphs report what each manifest installs, and pip installs only what a `-c` target pins, not the rest of it.
69
+ # Checked last, as the package manager is memoised and must see any generated lockfile.
70
+ file_parser.options[:requirement_roots] = true if supports_layering?
66
71
  super
67
72
  end
68
73
 
@@ -174,11 +179,10 @@ module Dependabot
174
179
  end
175
180
 
176
181
  # Resolves the `.txt` files referenced from a requirements file via `-r`/`-c`, returning their names
177
- # relative to the repo (matching the fetched DependencyFile names) so we can retain them. Delegates to
178
- # RequirementsLayers.referenced_paths so grouping and the bystander filter resolve references identically.
182
+ # relative to the repo (matching the fetched DependencyFile names) so we can retain them.
179
183
  sig { params(file: Dependabot::DependencyFile).returns(T::Array[String]) }
180
184
  def referenced_txt_names(file)
181
- RequirementsLayers.referenced_paths(file).select { |path| path.end_with?(".txt") }
185
+ RequirementReferences.paths(file).select { |path| path.end_with?(".txt") }
182
186
  end
183
187
 
184
188
  sig { returns(T::Boolean) }
@@ -20,6 +20,8 @@ module Dependabot
20
20
  const :source_requirement, T.nilable(String), default: nil
21
21
  const :extras, T::Array[String]
22
22
  const :requirement_type, T.nilable(String), default: nil
23
+ # Reached through a `-c` line, so it pins a version without adding a requirement.
24
+ const :constraint, T::Boolean, default: false
23
25
 
24
26
  sig { params(result: Object).returns(T::Array[PepDependency]) }
25
27
  def self.from_helper_result(result)
@@ -56,7 +58,8 @@ module Dependabot
56
58
  requirement_type: PyprojectValueParser.optional_string(
57
59
  hash["requirement_type"],
58
60
  "PEP dependency requirement_type"
59
- )
61
+ ),
62
+ constraint: hash["constraint"] == true
60
63
  )
61
64
  end
62
65
  end
@@ -0,0 +1,124 @@
1
+ # typed: strong
2
+ # frozen_string_literal: true
3
+
4
+ require "dependabot/python/file_parser/poetry_lock"
5
+
6
+ module Dependabot
7
+ module Python
8
+ class FileParser < Dependabot::FileParsers::Base
9
+ class PoetryLock
10
+ class GitSource < T::ImmutableStruct
11
+ const :url, String
12
+ const :reference, String
13
+ const :subdirectory, T.nilable(String), default: nil
14
+ end
15
+
16
+ class Scope < T::ImmutableStruct
17
+ extend T::Sig
18
+
19
+ const :optional, T::Boolean
20
+ const :groups, T::Array[String]
21
+ const :markers, T::Hash[String, T::Array[String]]
22
+
23
+ sig { params(group: String).returns(T::Array[String]) }
24
+ def markers_for(group)
25
+ markers.fetch(group, [])
26
+ end
27
+ end
28
+
29
+ class Record
30
+ extend T::Sig
31
+
32
+ sig { params(data: Object).void }
33
+ def initialize(data)
34
+ @data = data
35
+ @object = T.let(nil, T.nilable(PyprojectValueParser::ObjectHash))
36
+ end
37
+
38
+ sig { returns(Package) }
39
+ def to_package
40
+ Package.new(
41
+ name: PyprojectValueParser.string(object["name"], "Poetry lock package name"),
42
+ version: version,
43
+ source_type: source_type
44
+ )
45
+ end
46
+
47
+ sig { returns(T.nilable(String)) }
48
+ def name
49
+ PyprojectValueParser.optional_string(object["name"], "Poetry lock package name")
50
+ end
51
+
52
+ sig { returns(String) }
53
+ def version
54
+ PyprojectValueParser.string(object["version"], "Poetry lock package version")
55
+ end
56
+
57
+ sig { returns(T.nilable(String)) }
58
+ def source_type
59
+ PyprojectValueParser.optional_string(source&.[]("type"), "Poetry lock package source type")
60
+ end
61
+
62
+ sig { returns(GitSource) }
63
+ def git_source
64
+ fields = source || {}
65
+ GitSource.new(
66
+ url: PyprojectValueParser.string(fields["url"], "Poetry lock package source url"),
67
+ reference: PyprojectValueParser.string(fields["reference"], "Poetry lock package source reference"),
68
+ subdirectory: PyprojectValueParser.optional_string(
69
+ fields["subdirectory"], "Poetry lock package source subdirectory"
70
+ )
71
+ )
72
+ end
73
+
74
+ sig { returns(Scope) }
75
+ def scope
76
+ groups_value = object["groups"]
77
+ groups = if groups_value.nil?
78
+ []
79
+ elsif groups_value.is_a?(String)
80
+ [groups_value]
81
+ else
82
+ PyprojectValueParser.string_array(groups_value, "Poetry lock package groups")
83
+ end
84
+ groups = ["main"] if groups.empty?
85
+
86
+ optional = object["optional"]
87
+ unless optional.nil? || optional == true || optional == false
88
+ raise TypeError, "Poetry lock package optional must be a boolean"
89
+ end
90
+
91
+ Scope.new(optional: optional == true, groups: groups, markers: markers_for(groups))
92
+ end
93
+
94
+ private
95
+
96
+ sig { returns(PyprojectValueParser::ObjectHash) }
97
+ def object
98
+ @object ||= PyprojectValueParser.object_hash(@data, "Poetry lock package")
99
+ end
100
+
101
+ sig { returns(T.nilable(PyprojectValueParser::ObjectHash)) }
102
+ def source
103
+ value = object["source"]
104
+ PyprojectValueParser.object_hash(value, "Poetry lock package source") unless value.nil?
105
+ end
106
+
107
+ sig { params(groups: T::Array[String]).returns(T::Hash[String, T::Array[String]]) }
108
+ def markers_for(groups)
109
+ value = object["markers"]
110
+ if value.is_a?(Hash)
111
+ PyprojectValueParser.object_hash(value, "Poetry lock package markers").to_h do |group, marker|
112
+ parsed = PyprojectValueParser.string(marker, "Poetry lock package markers.#{group}")
113
+ [group, ["(#{parsed})"]]
114
+ end
115
+ else
116
+ marker = PyprojectValueParser.optional_string(value, "Poetry lock package markers")
117
+ groups.to_h { |group| [group, marker ? ["(#{marker})"] : []] }
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -14,6 +14,8 @@ module Dependabot
14
14
  class PoetryLock
15
15
  extend T::Sig
16
16
 
17
+ require_relative "poetry_lock/record"
18
+
17
19
  class Package < T::ImmutableStruct
18
20
  extend T::Sig
19
21
 
@@ -23,23 +25,7 @@ module Dependabot
23
25
 
24
26
  sig { params(value: Object).returns(Package) }
25
27
  def self.from_object(value)
26
- hash = PyprojectValueParser.object_hash(value, "Poetry lock package")
27
- source = hash["source"]
28
- source_hash =
29
- if source.nil?
30
- nil
31
- else
32
- PyprojectValueParser.object_hash(source, "Poetry lock package source")
33
- end
34
-
35
- new(
36
- name: PyprojectValueParser.string(hash["name"], "Poetry lock package name"),
37
- version: PyprojectValueParser.string(hash["version"], "Poetry lock package version"),
38
- source_type: PyprojectValueParser.optional_string(
39
- source_hash&.[]("type"),
40
- "Poetry lock package source type"
41
- )
42
- )
28
+ Record.new(value).to_package
43
29
  end
44
30
 
45
31
  sig { returns(T::Hash[Symbol, T.nilable(String)]) }
@@ -52,42 +38,90 @@ module Dependabot
52
38
  end
53
39
  end
54
40
 
55
- sig { returns(T::Array[Package]) }
56
- attr_reader :packages
57
-
58
- sig { params(packages: T::Array[Package]).void }
59
- def initialize(packages)
60
- @packages = packages
41
+ sig { params(data: PyprojectValueParser::ObjectHash).void }
42
+ def initialize(data)
43
+ @data = data
44
+ @packages = T.let(nil, T.nilable(T::Array[Package]))
45
+ @records = T.let(nil, T.nilable(T::Array[Record]))
61
46
  end
62
47
 
63
48
  sig { params(file: Dependabot::DependencyFile).returns(PoetryLock) }
64
49
  def self.from_file(file)
65
- parsed = T.cast(TomlRB.parse(T.must(file.content)), Object)
66
- document = PyprojectValueParser.object_hash(parsed, "poetry.lock")
67
- value = document["package"]
68
- packages =
69
- if value.nil?
70
- []
71
- else
72
- PyprojectValueParser.array(value, "poetry.lock package").map do |package|
73
- Package.from_object(package)
74
- end
75
- end
76
- new(packages)
50
+ document = from_content(T.must(file.content))
51
+ document.packages
52
+ document
77
53
  rescue TomlRB::ParseError, TomlRB::ValueOverwriteError
78
54
  raise Dependabot::DependencyFileNotParseable, file.path
79
55
  end
80
56
 
57
+ sig { params(content: String).returns(PoetryLock) }
58
+ def self.from_content(content)
59
+ parsed = T.cast(TomlRB.parse(content), Object)
60
+ new(PyprojectValueParser.object_hash(parsed, "poetry.lock"))
61
+ end
62
+
63
+ sig { returns(T::Array[Package]) }
64
+ def packages
65
+ @packages ||= records.map(&:to_package)
66
+ end
67
+
68
+ sig do
69
+ params(name: String, _normalizer: T.proc.params(name: String).returns(String)).returns(T.nilable(Record))
70
+ end
71
+ def record_for(name, &_normalizer)
72
+ records.find do |record|
73
+ record_name = record.name
74
+ record_name && yield(record_name) == name
75
+ end
76
+ end
77
+
78
+ sig do
79
+ params(name: String, _normalizer: T.proc.params(name: String).returns(String)).returns(T::Array[Record])
80
+ end
81
+ def records_for(name, &_normalizer)
82
+ records.select do |record|
83
+ record_name = record.name
84
+ record_name && yield(record_name) == name
85
+ end
86
+ end
87
+
81
88
  sig do
82
89
  params(
83
90
  name: String,
84
- _normalizer: T.proc.params(name: String).returns(String)
91
+ normalizer: T.proc.params(name: String).returns(String)
85
92
  )
86
93
  .returns(T.nilable(String))
87
94
  end
88
- def version_for(name, &_normalizer)
89
- normalized_name = yield(name)
90
- packages.find { |package| yield(package.name) == normalized_name }&.version
95
+ def version_for(name, &normalizer)
96
+ record_for(yield(name), &normalizer)&.version
97
+ end
98
+
99
+ sig { returns(String) }
100
+ def python_versions
101
+ PyprojectValueParser.string(metadata["python-versions"], "poetry.lock metadata.python-versions")
102
+ end
103
+
104
+ sig { returns(String) }
105
+ def content_hash
106
+ PyprojectValueParser.string(metadata["content-hash"], "poetry.lock metadata.content-hash")
107
+ end
108
+
109
+ private
110
+
111
+ sig { returns(T::Array[Record]) }
112
+ def records
113
+ @records ||= if @data["package"].nil?
114
+ []
115
+ else
116
+ PyprojectValueParser.array(@data["package"], "poetry.lock package").map do |entry|
117
+ Record.new(entry)
118
+ end
119
+ end
120
+ end
121
+
122
+ sig { returns(PyprojectValueParser::ObjectHash) }
123
+ def metadata
124
+ PyprojectValueParser.object_hash(@data["metadata"], "poetry.lock metadata")
91
125
  end
92
126
  end
93
127
  end
@@ -0,0 +1,154 @@
1
+ # typed: strong
2
+ # frozen_string_literal: true
3
+
4
+ require "dependabot/python/file_parser/pyproject_document"
5
+
6
+ module Dependabot
7
+ module Python
8
+ class FileParser < Dependabot::FileParsers::Base
9
+ class PyprojectDocument
10
+ PEP508_PREFIX = /\A(?<prefix>(?<name>[a-zA-Z0-9](?:[a-zA-Z0-9._-]*[a-zA-Z0-9])?)(?:\[[^\]]*\])?)/i
11
+
12
+ sig { params(versions: T::Hash[String, String]).void }
13
+ def freeze_pep621_versions(versions)
14
+ dependency_arrays(include_groups: false, strict: false).each do |entries|
15
+ next unless entries.all?(String)
16
+
17
+ entries.map! do |entry|
18
+ declaration = PyprojectValueParser.string(entry, "PEP 621 dependency")
19
+ match = declaration.match(PEP508_PREFIX)
20
+ version = match && versions[NameNormaliser.normalise(T.must(match[:name]))]
21
+ version ? pin_pep508_entry(declaration, version) : declaration
22
+ end
23
+ end
24
+ end
25
+
26
+ sig { params(lock: PoetryLock, excluding: T::Array[String]).void }
27
+ def freeze_locked_pep621_dependencies(lock:, excluding:)
28
+ dependency_arrays(include_groups: false, strict: true).each do |entries|
29
+ entries.map! do |entry|
30
+ declaration = PyprojectValueParser.string(entry, "PEP 621 dependency")
31
+ match = declaration.match(PEP508_PREFIX)
32
+ next declaration unless match
33
+
34
+ name = NameNormaliser.normalise(T.must(match[:name]))
35
+ next declaration if excluding.include?(name)
36
+ next declaration if lock.records_for(name, &:itself).size > 1
37
+
38
+ record = lock.record_for(name, &:itself)
39
+ record ? pin_pep508_entry(declaration, record.version) : declaration
40
+ end
41
+ end
42
+ end
43
+
44
+ sig { params(name: String, requirement: String).returns(Integer) }
45
+ def replace_pep621_requirement(name:, requirement:)
46
+ changed = 0
47
+ dependency_arrays(include_groups: true, strict: false).each do |entries|
48
+ entries.map! do |entry|
49
+ next entry unless entry.is_a?(String)
50
+
51
+ match = entry.match(PEP508_PREFIX)
52
+ next entry unless match && NameNormaliser.normalise(T.must(match[:name])) == name
53
+
54
+ changed += 1
55
+ prefix = T.must(match[:prefix])
56
+ head, separator, tail = entry[prefix.length..].to_s.partition(";")
57
+ marker = separator.empty? ? "" : "#{head[head.rstrip.length..]};#{tail}"
58
+ "#{prefix}#{requirement}#{marker}"
59
+ end
60
+ end
61
+ changed
62
+ end
63
+
64
+ sig { params(main: T.nilable(String), extras: T::Hash[String, String], groups: T::Hash[String, String]).void }
65
+ def append_pep621_pins(main:, extras:, groups:)
66
+ project = mutable_section(@data, "project", "project")
67
+ return unless project
68
+
69
+ if main
70
+ entries = project["dependencies"]
71
+ dependencies = entries.nil? ? [] : PyprojectValueParser.array(entries, "project.dependencies")
72
+ project["dependencies"] = dependencies << main
73
+ end
74
+ optional = T.must(
75
+ mutable_section(project, "optional-dependencies", "project.optional-dependencies", create: true)
76
+ )
77
+ append_group_pins(optional, extras)
78
+ return if groups.empty?
79
+
80
+ table = T.must(mutable_section(@data, "dependency-groups", "dependency-groups", create: true))
81
+ append_group_pins(table, groups)
82
+ end
83
+
84
+ private
85
+
86
+ sig { params(include_groups: T::Boolean, strict: T::Boolean).returns(T::Array[T::Array[Object]]) }
87
+ def dependency_arrays(include_groups:, strict:)
88
+ project = mutable_section(@data, "project", "project")
89
+ return [] unless project
90
+
91
+ arrays = T.let([], T::Array[T::Array[Object]])
92
+ main = attached_array(project, "dependencies", "project.dependencies", strict: strict)
93
+ arrays << main if main
94
+ optional = mutable_section(project, "optional-dependencies", "project.optional-dependencies")
95
+ optional&.each_key do |name|
96
+ entries = attached_array(optional, name, "project.optional-dependencies.#{name}", strict: strict)
97
+ arrays << entries if entries
98
+ end
99
+ if include_groups
100
+ groups = mutable_section(@data, "dependency-groups", "dependency-groups")
101
+ groups&.each_key do |name|
102
+ entries = attached_array(groups, name, "dependency-groups.#{name}", strict: strict)
103
+ arrays << entries if entries
104
+ end
105
+ end
106
+ arrays
107
+ end
108
+
109
+ sig do
110
+ params(
111
+ parent: PyprojectValueParser::ObjectHash, key: String, context: String, strict: T::Boolean
112
+ ).returns(T.nilable(T::Array[Object]))
113
+ end
114
+ def attached_array(parent, key, context, strict:)
115
+ value = parent[key]
116
+ return if value.nil? || (!strict && !value.is_a?(Array))
117
+
118
+ entries = PyprojectValueParser.array(value, context)
119
+ parent[key] = entries
120
+ entries
121
+ end
122
+
123
+ sig { params(table: PyprojectValueParser::ObjectHash, pins: T::Hash[String, String]).void }
124
+ def append_group_pins(table, pins)
125
+ pins.each do |name, pin|
126
+ key = table.keys.find { |entry| normalized_group(entry) == normalized_group(name) } || name
127
+ value = table[key]
128
+ entries = value.nil? ? [] : PyprojectValueParser.array(value, "dependency group #{key}")
129
+ table[key] = entries << pin
130
+ end
131
+ end
132
+
133
+ sig { params(name: String).returns(String) }
134
+ def normalized_group(name)
135
+ name.downcase.gsub(/[-_.]+/, "-")
136
+ end
137
+
138
+ sig { params(entry: String, version: String).returns(String) }
139
+ def pin_pep508_entry(entry, version)
140
+ match = entry.match(PEP508_PREFIX)
141
+ return entry unless match
142
+
143
+ prefix = T.must(match[:prefix])
144
+ rest = T.must(entry[prefix.length..])
145
+ return entry if rest.match?(/\A\s*@\s/)
146
+
147
+ marker_match = rest.match(/(\s*;.*)/)
148
+ marker = marker_match ? marker_match[1] : ""
149
+ "#{prefix}==#{version}#{marker}"
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end