diff --git a/Library/Homebrew/test/utils/pypi_spec.rb b/Library/Homebrew/test/utils/pypi_spec.rb
index 20f423df52aef816b7d57d6030e7d2fd6e3b404d..25e733c19249d93cc1b51b165d7728af7a5b4904 100644
--- a/Library/Homebrew/test/utils/pypi_spec.rb
+++ b/Library/Homebrew/test/utils/pypi_spec.rb
@@ -172,5 +172,13 @@ describe PyPI do
     it "updates url to new version" do
       expect(described_class.update_pypi_url(old_package_url, "5.29.0")).to eq package_url
     end
+
+    it "returns nil for invalid versions" do
+      expect(described_class.update_pypi_url(old_package_url, "0.0.0")).to eq nil
+    end
+
+    it "returns nil for non-pypi urls" do
+      expect(described_class.update_pypi_url("https://brew.sh/foo-1.0.tgz", "1.1")).to eq nil
+    end
   end
 end
diff --git a/Library/Homebrew/utils/pypi.rb b/Library/Homebrew/utils/pypi.rb
index 7aeef94c29a1bc513de02f6910d26c115a6d2c0f..365f79245fe00f5561cbad9a9a7236d5ac3bcabd 100644
--- a/Library/Homebrew/utils/pypi.rb
+++ b/Library/Homebrew/utils/pypi.rb
@@ -110,8 +110,12 @@ module PyPI
   def update_pypi_url(url, version)
     package = Package.new url, is_url: true
 
+    return unless package.valid_pypi_package?
+
     _, url = package.pypi_info(version: version)
     url
+  rescue ArgumentError
+    nil
   end
 
   # Return true if resources were checked (even if no change).