language/python: enable typed: strict

Signed-off-by: Michael Cho <michael@michaelcho.dev>
This commit is contained in:
Michael Cho 2024-03-29 18:32:43 -04:00
parent 61f8ebc462
commit 7931b3cb12
No known key found for this signature in database
GPG Key ID: 55E85E28A7CD1E85

View File

@ -1,4 +1,4 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module Language module Language
@ -28,6 +28,12 @@ module Language
end end
end end
sig {
params(
build: T.any(BuildOptions, Tab),
block: T.nilable(T.proc.params(python: String, version: T.nilable(Version)).void),
).void
}
def self.each_python(build, &block) def self.each_python(build, &block)
original_pythonpath = ENV.fetch("PYTHONPATH", nil) original_pythonpath = ENV.fetch("PYTHONPATH", nil)
pythons = { "python@3" => "python3", pythons = { "python@3" => "python3",
@ -48,6 +54,7 @@ module Language
ENV["PYTHONPATH"] = original_pythonpath ENV["PYTHONPATH"] = original_pythonpath
end end
sig { params(python: T.any(String, Pathname)).returns(T::Boolean) }
def self.reads_brewed_pth_files?(python) def self.reads_brewed_pth_files?(python)
return false unless homebrew_site_packages(python).directory? return false unless homebrew_site_packages(python).directory?
return false unless homebrew_site_packages(python).writable? return false unless homebrew_site_packages(python).writable?
@ -61,10 +68,12 @@ module Language
end end
end end
sig { params(python: T.any(String, Pathname)).returns(Pathname) }
def self.user_site_packages(python) def self.user_site_packages(python)
Pathname.new(`#{python} -c "import site; print(site.getusersitepackages())"`.chomp) Pathname.new(`#{python} -c "import site; print(site.getusersitepackages())"`.chomp)
end end
sig { params(python: T.any(String, Pathname), path: T.any(String, Pathname)).returns(T::Boolean) }
def self.in_sys_path?(python, path) def self.in_sys_path?(python, path)
script = <<~PYTHON script = <<~PYTHON
import os, sys import os, sys
@ -102,7 +111,7 @@ module Language
PYTHON_SHEBANG_REGEX = %r{^#! ?/usr/bin/(?:env )?python(?:[23](?:\.\d{1,2})?)?( |$)} PYTHON_SHEBANG_REGEX = %r{^#! ?/usr/bin/(?:env )?python(?:[23](?:\.\d{1,2})?)?( |$)}
# The length of the longest shebang matching `SHEBANG_REGEX`. # The length of the longest shebang matching `SHEBANG_REGEX`.
PYTHON_SHEBANG_MAX_LENGTH = "#! /usr/bin/env pythonx.yyy ".length PYTHON_SHEBANG_MAX_LENGTH = T.let("#! /usr/bin/env pythonx.yyy ".length, Integer)
# @private # @private
sig { params(python_path: T.any(String, Pathname)).returns(Utils::Shebang::RewriteInfo) } sig { params(python_path: T.any(String, Pathname)).returns(Utils::Shebang::RewriteInfo) }
@ -254,7 +263,7 @@ module Language
sig { params(formula: Formula, venv_root: T.any(String, Pathname), python: T.any(String, Pathname)).void } sig { params(formula: Formula, venv_root: T.any(String, Pathname), python: T.any(String, Pathname)).void }
def initialize(formula, venv_root, python) def initialize(formula, venv_root, python)
@formula = formula @formula = formula
@venv_root = Pathname.new(venv_root) @venv_root = T.let(Pathname.new(venv_root), Pathname)
@python = python @python = python
end end