Skip to content

Commit

Permalink
language/python: enable typed: strict
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Cho <michael@michaelcho.dev>
  • Loading branch information
cho-m committed Mar 29, 2024
1 parent 6c03d6d commit 83ab63c
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Library/Homebrew/language/python.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typed: true
# typed: strict
# frozen_string_literal: true

module Language
Expand Down Expand Up @@ -28,6 +28,12 @@ def self.site_packages(python = "python3.7")
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)
original_pythonpath = ENV.fetch("PYTHONPATH", nil)
pythons = { "python@3" => "python3",
Expand All @@ -48,6 +54,7 @@ def self.each_python(build, &block)
ENV["PYTHONPATH"] = original_pythonpath
end

sig { params(python: T.any(String, Pathname)).returns(T::Boolean) }
def self.reads_brewed_pth_files?(python)
return false unless homebrew_site_packages(python).directory?
return false unless homebrew_site_packages(python).writable?
Expand All @@ -61,10 +68,12 @@ def self.reads_brewed_pth_files?(python)
end
end

sig { params(python: T.any(String, Pathname)).returns(Pathname) }
def self.user_site_packages(python)
Pathname.new(`#{python} -c "import site; print(site.getusersitepackages())"`.chomp)
end

sig { params(python: T.any(String, Pathname), path: T.any(String, Pathname)).returns(T::Boolean) }
def self.in_sys_path?(python, path)
script = <<~PYTHON
import os, sys
Expand Down Expand Up @@ -102,7 +111,7 @@ module Shebang
PYTHON_SHEBANG_REGEX = %r{^#! ?/usr/bin/(?:env )?python(?:[23](?:\.\d{1,2})?)?( |$)}

# 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
sig { params(python_path: T.any(String, Pathname)).returns(Utils::Shebang::RewriteInfo) }
Expand Down Expand Up @@ -254,7 +263,7 @@ class Virtualenv
sig { params(formula: Formula, venv_root: T.any(String, Pathname), python: T.any(String, Pathname)).void }
def initialize(formula, venv_root, python)
@formula = formula
@venv_root = Pathname.new(venv_root)
@venv_root = T.let(Pathname.new(venv_root), Pathname)
@python = python
end

Expand Down

0 comments on commit 83ab63c

Please sign in to comment.