Skip to content

Commit a893dc4

Browse files
committed
Strictly type uv PyProjectPreparer
1 parent ed4fb5a commit a893dc4

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

uv/lib/dependabot/uv/file_updater/pyproject_preparer.rb

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# typed: true
1+
# typed: strong
22
# frozen_string_literal: true
33

44
require "toml-rb"
@@ -15,21 +15,26 @@ module Dependabot
1515
module Uv
1616
class FileUpdater
1717
class PyprojectPreparer
18+
extend T::Sig
19+
20+
Credentials = T.type_alias { T::Array[T::Hash[String, String]] }
21+
22+
sig { params(pyproject_content: String, lockfile: T.nilable(Dependabot::DependencyFile)).void }
1823
def initialize(pyproject_content:, lockfile: nil)
1924
@pyproject_content = pyproject_content
2025
@lockfile = lockfile
21-
@lines = pyproject_content.split("\n")
26+
@lines = T.let(pyproject_content.split("\n"), T::Array[String])
2227
end
2328

29+
sig { params(python_version: T.nilable(String)).returns(String) }
2430
def update_python_requirement(python_version)
2531
return @pyproject_content unless python_version
2632

27-
in_project_table = false
28-
updated_lines = @lines.map.with_index do |line, _i|
29-
if line.match?(/^\[project\]/)
30-
in_project_table = true
31-
line
32-
elsif in_project_table && line.match?(/^requires-python\s*=/)
33+
in_project_table = T.let(false, T::Boolean)
34+
updated_lines = @lines.map do |line|
35+
in_project_table = true if line.match?(/^\[project\]/)
36+
37+
if in_project_table && line.match?(/^requires-python\s*=/)
3338
"requires-python = \">=#{python_version}\""
3439
else
3540
line
@@ -39,6 +44,7 @@ def update_python_requirement(python_version)
3944
@pyproject_content = updated_lines.join("\n")
4045
end
4146

47+
sig { params(credentials: T.nilable(Credentials)).returns(T.nilable(Credentials)) }
4248
def add_auth_env_vars(credentials)
4349
return unless credentials
4450

@@ -58,15 +64,18 @@ def add_auth_env_vars(credentials)
5864
end
5965
end
6066

67+
sig { returns(String) }
6168
def sanitize
6269
# No special sanitization needed for UV files at this point
6370
@pyproject_content
6471
end
6572

6673
private
6774

75+
sig { returns(T.nilable(Dependabot::DependencyFile)) }
6876
attr_reader :lockfile
6977

78+
sig { params(url: String).returns(String) }
7079
def sanitize_env_name(url)
7180
url.gsub(%r{^https?://}, "").gsub(/[^a-zA-Z0-9]/, "_").upcase
7281
end

0 commit comments

Comments
 (0)