dependabot-python 0.402.0 → 0.404.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: eae7152a290937aaf3118091bf2d2befbeaeb54cf625498312167ea0cc88d867
4
- data.tar.gz: d612db59f7e6ac54053c7a1a2f27a2fd591312a5292bfabd088d05a402acb453
3
+ metadata.gz: 658f2f5ddb3748a69e82dc4f8bf49d767a2c0dae9cbb8914a593d20ef5c07ea3
4
+ data.tar.gz: 68b2055158a4c722df9189946fbf1a187fcf30772d891c34656ee528d77f5301
5
5
  SHA512:
6
- metadata.gz: 52df615381fec79610648a6e131cbe10f25ecccacb23a0b1b4ae54e1557eda648ae784181b110e609f670762a536768110ca71ee7602c3889734b295bf560994
7
- data.tar.gz: 73aad5f18f8cdf0293d65dd57a1f71aa5d6da1afab438258c091c25dd09d1f0949535b1da729643deebb80dcd89ea4bc0785a20344421d975d2b782c4085895d
6
+ metadata.gz: 94b57c466aca413a45ac4cf7d467c3e6267e44e045a7d60d5c81d938f3ab272022b01cf678842350cc9ec22e6292b77698d562fc368061302581f3a60dd80eed
7
+ data.tar.gz: 804c699ef9572ecba46b5de36b11d25fdd06f869e514f411f3e82cf109ff829855a38ec333d523ee49d1dfa9e8e1270d8d50eb147f1839e86ad6dd54808be4c7
@@ -2,7 +2,9 @@ pip==26.2.1
2
2
  pip-tools==7.6.1
3
3
  flake8==7.4.1
4
4
  hashin==1.0.5
5
- pipenv==2024.4.1
5
+ # pipenv 2024.4.1 fails to import on Python 3.15 (typing.no_type_check_decorator was removed)
6
+ pipenv==2024.4.1; python_version < "3.15"
7
+ pipenv==2025.1.1; python_version >= "3.15"
6
8
  plette==2.2.1
7
9
  poetry==2.5.1
8
10
  pytest==9.1.1
@@ -121,11 +121,24 @@ module Dependabot
121
121
  new(PyprojectValueParser.object_hash(parsed, "pyproject.toml"))
122
122
  end
123
123
 
124
+ POETRY_BUILD_BACKEND_PREFIX = "poetry.core"
125
+
124
126
  sig { returns(T::Boolean) }
125
127
  def poetry?
126
128
  !poetry_root.nil?
127
129
  end
128
130
 
131
+ # Poetry 2.0+ allows projects to declare dependencies using only PEP 621's
132
+ # [project.dependencies], with no [tool.poetry] table at all. We detect these
133
+ # projects via their declared build backend so they're still treated as Poetry
134
+ # projects (e.g. for lockfile parsing and dependency updates).
135
+ sig { returns(T::Boolean) }
136
+ def poetry_build_backend?
137
+ build_system = section(@data, "build-system", "build-system")
138
+ backend = build_system&.[]("build-backend")
139
+ backend.is_a?(String) && backend.start_with?(POETRY_BUILD_BACKEND_PREFIX)
140
+ end
141
+
129
142
  sig { returns(T::Boolean) }
130
143
  def project?
131
144
  !section(@data, "project", "project").nil?
@@ -275,6 +288,32 @@ module Dependabot
275
288
  !(keys.include?("git") && keys.include?("tag"))
276
289
  end
277
290
 
291
+ # Keys come back as the author wrote them, which is what a caller has to match the file on.
292
+ sig { returns(T::Hash[String, T::Hash[Symbol, String]]) }
293
+ def uv_git_tag_sources
294
+ tool = section(@data, "tool", "tool")
295
+ uv = tool && section(tool, "uv", "tool.uv")
296
+ sources = uv && section(uv, "sources", "tool.uv.sources")
297
+ return {} unless sources
298
+
299
+ result = T.let({}, T::Hash[String, T::Hash[Symbol, String]])
300
+ sources.each do |name, config|
301
+ next unless config.is_a?(Hash)
302
+
303
+ entry = PyprojectValueParser.object_hash(config, "tool.uv.sources.#{name}")
304
+ url = entry["git"]
305
+ ref = entry["tag"]
306
+ next unless url.is_a?(String) && ref.is_a?(String)
307
+
308
+ result[name] = { url: url, ref: ref }
309
+ end
310
+ result
311
+ rescue TypeError
312
+ # A malformed table is not this method's business either: it runs for every PEP 621
313
+ # manifest, so raising would stop every dependency in the repository being updated.
314
+ {}
315
+ end
316
+
278
317
  sig { params(key: String).returns(T::Array[String]) }
279
318
  def workspace_globs(key)
280
319
  tool = section(@data, "tool", "tool")
@@ -207,7 +207,19 @@ module Dependabot
207
207
 
208
208
  sig { returns(T::Boolean) }
209
209
  def using_poetry?
210
- pyproject_document.poetry?
210
+ pyproject_document.poetry? || locked_poetry_build_backend?
211
+ end
212
+
213
+ # A declared Poetry build backend alone isn't proof the project is actively Poetry-managed;
214
+ # require a committed lockfile as evidence it's really locked by Poetry.
215
+ sig { returns(T::Boolean) }
216
+ def locked_poetry_build_backend?
217
+ pyproject_document.poetry_build_backend? && !lockfile.nil?
218
+ end
219
+
220
+ sig { returns(T::Boolean) }
221
+ def locked_pep621_only_poetry?
222
+ !pyproject_document.poetry? && locked_poetry_build_backend?
211
223
  end
212
224
 
213
225
  sig { returns(T::Boolean) }
@@ -233,8 +245,9 @@ module Dependabot
233
245
  sig { params(dep: PepDependency).returns(T::Boolean) }
234
246
  def skip_pep621_dep?(dep)
235
247
  # If a requirement has a `<` or `<=` marker then updating it is
236
- # probably blocked. Ignore it.
237
- return true if dep.markers&.include?("<")
248
+ # probably blocked. Ignore it, unless Poetry has locked it: skipping would
249
+ # re-surface it from the lockfile as a requirement-less sub-dependency.
250
+ return true if dep.markers&.include?("<") && !locked_pep621_only_poetry?
238
251
 
239
252
  # If no requirement, don't add it
240
253
  requirement = dep.requirement
@@ -15,6 +15,7 @@ require "dependabot/python/name_normaliser"
15
15
  require "dependabot/python/pip_compile_file_matcher"
16
16
  require "dependabot/python/language_version_manager"
17
17
  require "dependabot/python/package_manager"
18
+ require "dependabot/python/marker_evaluator"
18
19
 
19
20
  module Dependabot
20
21
  module Python
@@ -351,8 +352,8 @@ module Dependabot
351
352
 
352
353
  version = python_raw_version
353
354
 
354
- if marker.include?("python_version")
355
- !marker_satisfied?(marker, version)
355
+ if marker.match?(/\bpython(?:_full)?_version\b/)
356
+ !marker_evaluator.marker_satisfied?(marker: marker, python_version: version)
356
357
  else
357
358
  return true if marker.include?("<")
358
359
  return false if marker.include?(">")
@@ -361,52 +362,9 @@ module Dependabot
361
362
  end
362
363
  end
363
364
 
364
- sig { params(marker: String, python_version: T.any(String, Integer, Gem::Version)).returns(T::Boolean) }
365
- def marker_satisfied?(marker, python_version)
366
- conditions = marker.split(/\s+(and|or)\s+/)
367
-
368
- # Explicitly define the type of result as T::Boolean
369
- result = T.let(evaluate_condition(T.must(conditions.shift), python_version), T::Boolean)
370
-
371
- until conditions.empty?
372
- operator = conditions.shift
373
- next_condition = T.must(conditions.shift)
374
- next_result = evaluate_condition(next_condition, python_version)
375
-
376
- result = if operator == "and"
377
- result && next_result
378
- else
379
- result || next_result
380
- end
381
- end
382
-
383
- result
384
- end
385
-
386
- sig do
387
- params(
388
- condition: String,
389
- python_version: T.any(String, Integer, Gem::Version)
390
- ).returns(T::Boolean)
391
- end
392
- def evaluate_condition(condition, python_version)
393
- operator, version = condition.match(/([<>=!]=?)\s*"?([\d.]+)"?/)&.captures
394
- return false unless version
395
-
396
- case operator
397
- when "<"
398
- Dependabot::Python::Version.new(python_version) < Dependabot::Python::Version.new(version)
399
- when "<="
400
- Dependabot::Python::Version.new(python_version) <= Dependabot::Python::Version.new(version)
401
- when ">"
402
- Dependabot::Python::Version.new(python_version) > Dependabot::Python::Version.new(version)
403
- when ">="
404
- Dependabot::Python::Version.new(python_version) >= Dependabot::Python::Version.new(version)
405
- when "=="
406
- Dependabot::Python::Version.new(python_version) == Dependabot::Python::Version.new(version)
407
- else
408
- false
409
- end
365
+ sig { returns(MarkerEvaluator) }
366
+ def marker_evaluator
367
+ @marker_evaluator ||= T.let(MarkerEvaluator.new, T.nilable(MarkerEvaluator))
410
368
  end
411
369
 
412
370
  sig { returns(DependencySet) }
@@ -101,10 +101,9 @@ module Dependabot
101
101
  ).returns(T.nilable(String))
102
102
  end
103
103
  def replace_with_source_requirement(content, source_req, new_r, old_r)
104
- match = content.match(declaration_regex(source_req))
105
- return unless match
104
+ declaration = find_declaration(content, declaration_regex(source_req))
105
+ return unless declaration
106
106
 
107
- declaration = T.must(match[:declaration])
108
107
  new_req_str = rewrite_pep508_requirement(
109
108
  source_req,
110
109
  T.must(old_r.requirement_string),
@@ -128,23 +127,44 @@ module Dependabot
128
127
  old_req = T.must(old_r.requirement_string)
129
128
  new_req = T.must(new_r.requirement_string)
130
129
 
131
- match = content.match(normalized_declaration_regex(old_req))
132
- return unless match
133
-
134
- declaration = T.must(match[:declaration])
135
- return unless declaration.include?(old_req)
130
+ declaration = find_declaration(content, normalized_declaration_regex(old_req))
131
+ return unless declaration&.include?(old_req)
136
132
 
137
133
  content.sub(declaration, declaration.sub(old_req, new_req))
138
134
  end
139
135
 
136
+ # The regexes stop after the requirement. The closing quote and any environment marker are
137
+ # checked here, since matching an unbounded marker in the regex itself can backtrack badly.
138
+ sig { params(content: String, prefix_regex: Regexp).returns(T.nilable(String)) }
139
+ def find_declaration(content, prefix_regex)
140
+ position = 0
141
+ while (match = content.match(prefix_regex, position))
142
+ finish = declaration_end(content, match.end(0))
143
+ return content[match.begin(0)...finish] if finish
144
+
145
+ position = match.begin(0) + 1
146
+ end
147
+ end
148
+
149
+ sig { params(content: String, start: Integer).returns(T.nilable(Integer)) }
150
+ def declaration_end(content, start)
151
+ tail = content[start..].to_s
152
+ quote = tail.index(/["']/)
153
+ return unless quote
154
+
155
+ trailing = tail[0, quote].to_s
156
+ start + quote + 1 if trailing.empty? || trailing.lstrip.start_with?(";")
157
+ end
158
+
140
159
  sig { params(old_req: String).returns(Regexp) }
141
160
  def declaration_regex(old_req)
142
- /(?<declaration>["']#{escape}\s*#{extras_pattern}\s*#{Regexp.escape(old_req)}["'])/mi
161
+ spacing = extras_pattern.empty? ? "\\s*" : "\\s*#{extras_pattern}\\s*"
162
+ /["']#{escape}#{spacing}#{Regexp.escape(old_req)}/mi
143
163
  end
144
164
 
145
165
  sig { params(old_req: String).returns(Regexp) }
146
166
  def normalized_declaration_regex(old_req)
147
- /(?<declaration>["']#{escape}#{extras_pattern}#{Regexp.escape(old_req)}["'])/mi
167
+ /["']#{escape}#{extras_pattern}#{Regexp.escape(old_req)}/mi
148
168
  end
149
169
 
150
170
  sig { returns(String) }
@@ -11,9 +11,11 @@ require "dependabot/python/version"
11
11
  require "dependabot/python/requirement"
12
12
  require "dependabot/python/file_parser/python_requirement_parser"
13
13
  require "dependabot/python/file_updater"
14
+ require "dependabot/python/file_parser/pyproject_document"
14
15
  require "dependabot/python/native_helpers"
15
16
  require "dependabot/python/name_normaliser"
16
17
  require "dependabot/python/poetry_plugin_installer"
18
+ require "dependabot/python/poetry_subdependency_pin"
17
19
  require "dependabot/package/release_cooldown_options"
18
20
 
19
21
  module Dependabot
@@ -29,6 +31,7 @@ module Dependabot
29
31
  end
30
32
 
31
33
  class FileUpdater
34
+ # rubocop:disable-next Metrics/ClassLength
32
35
  class PoetryFileUpdater
33
36
  require_relative "pyproject_preparer"
34
37
  require_relative "poetry_file_updater/pep621_updater"
@@ -249,6 +252,8 @@ module Dependabot
249
252
  create_declaration_at_new_version!(poetry_object, dep)
250
253
  end
251
254
  end
255
+ elsif FileParser::PyprojectDocument.from_content(pyproject_content).poetry_build_backend?
256
+ pin_pep621_subdependencies!(pyproject_object)
252
257
  end
253
258
 
254
259
  # Freeze PEP 621 project.dependencies and project.optional-dependencies
@@ -259,6 +264,21 @@ module Dependabot
259
264
  TomlRB.dump(pyproject_object)
260
265
  end
261
266
 
267
+ # freeze_pep621_deps! skips dependency groups and a sub-dependency has no declaration, so without
268
+ # this Poetry could lock either to a newer release than the one selected.
269
+ sig { params(pyproject_object: T::Hash[String, T.untyped]).void }
270
+ def pin_pep621_subdependencies!(pyproject_object)
271
+ dependencies.each do |dep|
272
+ next if git_dependency_being_updated?(dep)
273
+ next unless dep.version
274
+
275
+ PoetrySubdependencyPin.new(lockfile, dep.name).set_requirement!(pyproject_object, "==#{dep.version}")
276
+ rescue PoetrySubdependencyPin::ScopeNotReproducible
277
+ # The update checker declines these; skipping keeps a direct run from promoting the package.
278
+ next
279
+ end
280
+ end
281
+
262
282
  sig { params(pyproject_content: String).returns(String) }
263
283
  def update_python_requirement(pyproject_content)
264
284
  PyprojectPreparer.new(pyproject_content: pyproject_content)
@@ -8,6 +8,7 @@ require "dependabot/python/file_parser"
8
8
  require "dependabot/python/file_updater"
9
9
  require "dependabot/python/authed_url_builder"
10
10
  require "dependabot/python/name_normaliser"
11
+ require "dependabot/python/requirement"
11
12
  require "securerandom"
12
13
 
13
14
  module Dependabot
@@ -122,8 +123,8 @@ module Dependabot
122
123
  pyproject_object = TomlRB.parse(@pyproject_content)
123
124
 
124
125
  if (python_specification = pyproject_object.dig("tool", "poetry", "dependencies", "python"))
125
- python_req = Python::Requirement.new(python_specification)
126
- unless python_req.satisfied_by?(requirement)
126
+ python_requirements = Python::Requirement.requirements_array(python_specification)
127
+ unless python_requirements.empty? || python_requirements.any? { |req| req.satisfied_by?(requirement) }
127
128
  pyproject_object["tool"]["poetry"]["dependencies"]["python"] = "~#{requirement}"
128
129
  end
129
130
  end
@@ -148,8 +149,9 @@ module Dependabot
148
149
  pyproject_object = TomlRB.parse(pyproject_content)
149
150
  poetry_object = pyproject_object.dig("tool", "poetry")
150
151
 
151
- return pyproject_content unless poetry_object
152
+ return pyproject_content unless poetry_object || locked_poetry_build_backend?
152
153
 
154
+ poetry_object ||= {}
153
155
  excluded_names = dependencies.map(&:name) + ["python"]
154
156
 
155
157
  Dependabot::Python::FileParser::PyprojectFilesParser::POETRY_DEPENDENCY_TYPES.each do |key|
@@ -176,6 +178,11 @@ module Dependabot
176
178
  sig { returns(T.nilable(Dependabot::DependencyFile)) }
177
179
  attr_reader :lockfile
178
180
 
181
+ sig { returns(T::Boolean) }
182
+ def locked_poetry_build_backend?
183
+ !lockfile.nil? && FileParser::PyprojectDocument.from_content(pyproject_content).poetry_build_backend?
184
+ end
185
+
179
186
  sig { params(deps_hash: T::Hash[String, T.untyped], dep_name: String).void }
180
187
  def freeze_poetry_dep!(deps_hash, dep_name)
181
188
  details = locked_details(dep_name)
@@ -230,6 +237,9 @@ module Dependabot
230
237
  dep_name = normalise(T.must(match[1]))
231
238
  next if excluded_names.include?(dep_name)
232
239
 
240
+ # One pin can't suit every Python branch of a package locked at several versions.
241
+ next if locked_entry_count(dep_name) > 1
242
+
233
243
  locked_details = locked_details(dep_name)
234
244
  next unless (locked_version = locked_details&.fetch("version"))
235
245
 
@@ -243,6 +253,11 @@ module Dependabot
243
253
  .find { |d| d["name"] == normalise(dep_name) }
244
254
  end
245
255
 
256
+ sig { params(dep_name: String).returns(Integer) }
257
+ def locked_entry_count(dep_name)
258
+ parsed_lockfile.fetch("package").count { |d| d["name"] == normalise(dep_name) }
259
+ end
260
+
246
261
  sig { params(name: String).returns(String) }
247
262
  def normalise(name)
248
263
  NameNormaliser.normalise(name)
@@ -150,7 +150,10 @@ module Dependabot
150
150
  after_escaped = Regexp.escape(after_name_extras)
151
151
  # Match the dependency name with any extras (or none), followed by the requirement.
152
152
  # This ensures a single gsub handles all extras variants of the same dependency.
153
- /(?<![\-\w\.\[])#{name_escaped}\s*\\?\s*(?:\[[^\]]*\])?\s*\\?\s*#{after_escaped}(?![\-\w\.])/
153
+ # The requirement must end where the declaration's own specifiers end: ">=2.7.0" is the start of
154
+ # ">=2.7.0,<3.0.0" and ">=2.7.0,!=2.9.0; sys_platform == 'win32'", which are other declarations.
155
+ /(?<![\-\w\.\[])#{name_escaped}\s*\\?\s*(?:\[[^\]]*\])?\s*\\?\s*
156
+ #{after_escaped}(?![\-\w\.]|\s*,\s*\\?\s*[<>=!~])/x
154
157
  end
155
158
 
156
159
  sig { params(requirement: T.nilable(String)).returns(T::Boolean) }
@@ -5,6 +5,7 @@ require "toml-rb"
5
5
  require "dependabot/file_updaters"
6
6
  require "dependabot/file_updaters/base"
7
7
  require "dependabot/shared_helpers"
8
+ require "dependabot/python/file_parser/pyproject_document"
8
9
  require "sorbet-runtime"
9
10
 
10
11
  module Dependabot
@@ -154,8 +155,7 @@ module Dependabot
154
155
 
155
156
  return false unless poetry_lock
156
157
 
157
- build_backend = parsed_pyproject.dig("build-system", "build-backend")
158
- !build_backend.nil? && build_backend.start_with?("poetry.core")
158
+ FileParser::PyprojectDocument.from_content(T.must(pyproject&.content)).poetry_build_backend?
159
159
  end
160
160
 
161
161
  sig { returns(T.nilable(Dependabot::DependencyFile)) }
@@ -16,11 +16,12 @@ module Dependabot
16
16
  # e.g. ARG PY_3_13=3.13.x
17
17
  # Note: uv ecosystem aliases this class, so updates here apply to both ecosystems.
18
18
  PRE_INSTALLED_PYTHON_VERSIONS_RAW = %w(
19
- 3.14.9
20
- 3.13.17
21
- 3.12.13
22
- 3.11.15
23
- 3.10.20
19
+ 3.15.2
20
+ 3.14.10
21
+ 3.13.18
22
+ 3.12.16
23
+ 3.11.18
24
+ 3.10.22
24
25
  ).freeze
25
26
 
26
27
  PRE_INSTALLED_PYTHON_VERSIONS = T.let(
@@ -0,0 +1,237 @@
1
+ # typed: strong
2
+ # frozen_string_literal: true
3
+
4
+ require "dependabot/python/version"
5
+
6
+ module Dependabot
7
+ module Python
8
+ class MarkerEvaluator
9
+ extend T::Sig
10
+
11
+ sig { params(requirement_string: String).returns([T.nilable(String), T.nilable(String)]) }
12
+ def split_requirement_and_marker(requirement_string)
13
+ separator_index = T.let(nil, T.nilable(Integer))
14
+ in_single_quote = T.let(false, T::Boolean)
15
+ in_double_quote = T.let(false, T::Boolean)
16
+
17
+ requirement_string.each_char.with_index do |char, index|
18
+ in_single_quote, in_double_quote, quote_toggled =
19
+ toggle_quote_state(char, in_single_quote, in_double_quote)
20
+ next if quote_toggled
21
+ next if in_single_quote || in_double_quote
22
+ next unless char == ";"
23
+
24
+ separator_index = index
25
+ break
26
+ end
27
+
28
+ return [requirement_string.strip, nil] if separator_index.nil?
29
+
30
+ requirement_part = requirement_string[0...separator_index]
31
+ marker_part = requirement_string[(separator_index + 1)..]
32
+
33
+ [requirement_part&.strip, marker_part&.strip]
34
+ end
35
+
36
+ sig { params(marker: String, python_version: String).returns(T::Boolean) }
37
+ def marker_satisfied?(marker:, python_version:)
38
+ evaluate_marker_expression(marker, python_version)
39
+ rescue ArgumentError
40
+ # If we cannot safely parse a python marker, treat it as applicable.
41
+ # This avoids silently skipping transitive constraints that may break installs.
42
+ true
43
+ end
44
+
45
+ private
46
+
47
+ sig { params(expression: String, python_version: String).returns(T::Boolean) }
48
+ def evaluate_marker_expression(expression, python_version)
49
+ expr = strip_wrapping_parentheses(expression.strip)
50
+
51
+ or_parts = split_top_level(expr, "or")
52
+ return or_parts.any? { |part| evaluate_marker_expression(part, python_version) } if or_parts.length > 1
53
+
54
+ and_parts = split_top_level(expr, "and")
55
+ if and_parts.length > 1
56
+ return and_parts.all? do |part|
57
+ evaluate_marker_expression(part, python_version) || !python_marker?(part)
58
+ end
59
+ end
60
+
61
+ not_expression = strip_top_level_not(expr)
62
+ if not_expression
63
+ return false unless python_marker?(not_expression)
64
+
65
+ return !evaluate_marker_expression(not_expression, python_version)
66
+ end
67
+
68
+ evaluate_python_version_condition(expr, python_version, default: python_marker?(expr))
69
+ end
70
+
71
+ sig { params(expression: String).returns(T.nilable(String)) }
72
+ def strip_top_level_not(expression)
73
+ return nil unless expression.start_with?("not")
74
+ return nil unless word_at?(expression, 0, "not")
75
+
76
+ remaining = expression[3..]&.strip
77
+ return nil if remaining.nil? || remaining.empty?
78
+
79
+ remaining
80
+ end
81
+
82
+ sig { params(expression: String).returns(T::Boolean) }
83
+ def python_marker?(expression)
84
+ expression.match?(/\bpython(?:_full)?_version\b/)
85
+ end
86
+
87
+ sig { params(expression: String).returns(String) }
88
+ def strip_wrapping_parentheses(expression)
89
+ expr = expression
90
+ while expr.start_with?("(") && expr.end_with?(")")
91
+ inner = expr[1...-1].to_s.strip
92
+ break unless balanced_parentheses?(inner)
93
+
94
+ expr = inner
95
+ end
96
+
97
+ expr
98
+ end
99
+
100
+ sig { params(expression: String).returns(T::Boolean) }
101
+ def balanced_parentheses?(expression)
102
+ depth = T.let(0, Integer)
103
+ in_single_quote = T.let(false, T::Boolean)
104
+ in_double_quote = T.let(false, T::Boolean)
105
+
106
+ expression.each_char do |char|
107
+ in_single_quote, in_double_quote, quote_toggled =
108
+ toggle_quote_state(char, in_single_quote, in_double_quote)
109
+ next if quote_toggled
110
+ next if in_single_quote || in_double_quote
111
+
112
+ depth += 1 if char == "("
113
+ depth -= 1 if char == ")"
114
+ return false if depth.negative?
115
+ end
116
+
117
+ depth.zero? && !in_single_quote && !in_double_quote
118
+ end
119
+
120
+ sig do
121
+ params(
122
+ char: String,
123
+ in_single_quote: T::Boolean,
124
+ in_double_quote: T::Boolean
125
+ ).returns([T::Boolean, T::Boolean, T::Boolean])
126
+ end
127
+ def toggle_quote_state(char, in_single_quote, in_double_quote)
128
+ return [!in_single_quote, in_double_quote, true] if char == "'" && !in_double_quote
129
+
130
+ return [in_single_quote, !in_double_quote, true] if char == '"' && !in_single_quote
131
+
132
+ [in_single_quote, in_double_quote, false]
133
+ end
134
+
135
+ sig { params(expression: String, operator: String).returns(T::Array[String]) }
136
+ def split_top_level(expression, operator)
137
+ parts = T.let([], T::Array[String])
138
+ token = T.let(+"", String)
139
+ depth = T.let(0, Integer)
140
+ in_single_quote = T.let(false, T::Boolean)
141
+ in_double_quote = T.let(false, T::Boolean)
142
+ i = T.let(0, Integer)
143
+
144
+ while i < expression.length
145
+ char = T.must(expression[i])
146
+
147
+ in_single_quote, in_double_quote, quote_toggled =
148
+ toggle_quote_state(char, in_single_quote, in_double_quote)
149
+ if quote_toggled
150
+ token << char
151
+ i += 1
152
+ next
153
+ end
154
+
155
+ depth = update_depth_for_unquoted_char(char, depth, in_single_quote, in_double_quote)
156
+
157
+ if depth.zero? && !in_single_quote && !in_double_quote && word_at?(expression, i, operator)
158
+ parts << token.strip
159
+ token = +""
160
+ i += operator.length
161
+ next
162
+ end
163
+
164
+ token << char
165
+ i += 1
166
+ end
167
+
168
+ parts << token.strip
169
+ parts
170
+ end
171
+
172
+ sig do
173
+ params(
174
+ char: String,
175
+ depth: Integer,
176
+ in_single_quote: T::Boolean,
177
+ in_double_quote: T::Boolean
178
+ ).returns(Integer)
179
+ end
180
+ def update_depth_for_unquoted_char(char, depth, in_single_quote, in_double_quote)
181
+ return depth if in_single_quote || in_double_quote
182
+
183
+ depth += 1 if char == "("
184
+ depth -= 1 if char == ")"
185
+ depth
186
+ end
187
+
188
+ sig { params(expression: String, index: Integer, word: String).returns(T::Boolean) }
189
+ def word_at?(expression, index, word)
190
+ return false unless expression[index, word.length] == word
191
+
192
+ before = index.zero? ? " " : T.must(expression[index - 1])
193
+ after_index = index + word.length
194
+ after = after_index >= expression.length ? " " : T.must(expression[after_index])
195
+
196
+ word_boundary?(before) && word_boundary?(after)
197
+ end
198
+
199
+ sig { params(char: String).returns(T::Boolean) }
200
+ def word_boundary?(char)
201
+ !!(char =~ /[^A-Za-z0-9_]/)
202
+ end
203
+
204
+ sig do
205
+ params(
206
+ condition: T.nilable(String),
207
+ python_version: String,
208
+ default: T::Boolean
209
+ ).returns(T::Boolean)
210
+ end
211
+ def evaluate_python_version_condition(condition, python_version, default:)
212
+ return default if condition.nil?
213
+
214
+ candidate = strip_wrapping_parentheses(condition.strip)
215
+ match = candidate.match(/\Apython(?:_full)?_version\s*(<=|>=|<|>|==|!=)\s*['\"]([^'\"]+)['\"]\z/)
216
+ return default unless match
217
+
218
+ operator = T.must(match[1])
219
+ version = T.must(match[2])
220
+ lhs = Dependabot::Python::Version.new(python_version)
221
+ rhs = Dependabot::Python::Version.new(version)
222
+
223
+ case operator
224
+ when "<" then lhs < rhs
225
+ when "<=" then lhs <= rhs
226
+ when ">" then lhs > rhs
227
+ when ">=" then lhs >= rhs
228
+ when "==" then lhs == rhs
229
+ when "!=" then lhs != rhs
230
+ else false
231
+ end
232
+ rescue ArgumentError
233
+ true
234
+ end
235
+ end
236
+ end
237
+ end