Skip to content

Commit

Permalink
Changed setup file typecheck as strict
Browse files Browse the repository at this point in the history
  • Loading branch information
randhircs committed Jan 16, 2025
1 parent 67b341b commit 99ac373
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions python/lib/dependabot/python/file_parser/setup_file_parser.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: true
# typed: strict
# frozen_string_literal: true

require "dependabot/dependency"
Expand All @@ -20,12 +20,14 @@ class SetupFileParser
TESTS_REQUIRE_REGEX = /tests_require\s*=\s*\[/m
EXTRAS_REQUIRE_REGEX = /extras_require\s*=\s*\{/m

CLOSING_BRACKET = { "[" => "]", "{" => "}" }.freeze
CLOSING_BRACKET = T.let({ "[" => "]", "{" => "}" }.freeze, T.any(T.untyped, T.untyped))

sig { params(dependency_files: T.untyped).void }
def initialize(dependency_files:)
@dependency_files = dependency_files
end

sig { returns(Dependabot::FileParsers::Base::DependencySet) }
def dependency_set
dependencies = Dependabot::FileParsers::Base::DependencySet.new

Expand Down Expand Up @@ -56,8 +58,10 @@ def dependency_set

private

sig { returns(T.untyped) }
attr_reader :dependency_files

sig { returns(T.untyped) }
def parsed_setup_file
SharedHelpers.in_a_temporary_directory do
write_temporary_dependency_files
Expand All @@ -79,6 +83,7 @@ def parsed_setup_file
parsed_sanitized_setup_file
end

sig { returns(T.nilable(T.any(T::Hash[String, T.untyped], String, T::Array[T::Hash[String, T.untyped]]))) }
def parsed_sanitized_setup_file
SharedHelpers.in_a_temporary_directory do
write_sanitized_setup_file
Expand All @@ -100,16 +105,18 @@ def parsed_sanitized_setup_file
[]
end

sig { params(requirements: T.untyped).returns(T.nilable(Python::Requirement)) }
def check_requirements(requirements)
requirements.each do |dep|
requirements&.each do |dep|
next unless dep["requirement"]

Python::Requirement.new(dep["requirement"].split(","))
T.let(Python::Requirement.new(dep["requirement"].split(",")), Python::Requirement)
rescue Gem::Requirement::BadRequirementError => e
raise Dependabot::DependencyFileNotEvaluatable, e.message
end
end

sig { void }
def write_temporary_dependency_files
dependency_files
.reject { |f| f.name == ".python-version" }
Expand All @@ -125,6 +132,7 @@ def write_temporary_dependency_files
# This sanitization is far from perfect (it will fail if any of the
# entries are dynamic), but it is an alternative approach to the one
# used in parser.py which sometimes succeeds when that has failed.
sig { void }
def write_sanitized_setup_file
install_requires = get_regexed_req_array(INSTALL_REQUIRES_REGEX)
setup_requires = get_regexed_req_array(SETUP_REQUIRES_REGEX)
Expand Down Expand Up @@ -175,6 +183,7 @@ def normalised_name(name, extras)
NameNormaliser.normalise_including_extras(name, extras)
end

sig { returns(T.untyped) }
def setup_file
dependency_files.find { |f| f.name == "setup.py" }
end
Expand Down

0 comments on commit 99ac373

Please sign in to comment.