hatch icon indicating copy to clipboard operation
hatch copied to clipboard

400 response, no error message

Open ghost opened this issue 1 year ago • 9 comments

(topfarm) ernie@andromeda:~/code/TopFarm2$ hatch publish -r test
dist/topfarm-2.3.7.post6.tar.gz ... failed
Error uploading to repository: https://test.pypi.org/legacy/ - Client error '400 Bad Request' for url 'https://test.pypi.org/legacy/'
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400
(topfarm) ernie@andromeda:~/code/TopFarm2$ pixi list | grep hatch
 WARN The feature 'tensorflow' is defined but not used in any environment
hatch                          1.13.0         pyhd8ed1ab_0           173 KiB     conda  hatch-1.13.0-pyhd8ed1ab_0.conda
hatchling                      1.26.3         pypyhff2d567_0         55.5 KiB    conda  hatchling-1.26.3-pypyhff2d567_0.conda
(topfarm) ernie@andromeda:~/code/TopFarm2$ 

The problem reappears with the latest release of hatchling. I would really appreciate hatch giving some human-readable feedback about the issue rather than status codes of http requests...

ghost avatar Nov 13 '24 11:11 ghost

Something like:

            response = self.client.post(
                self.repo,
                data=data,
                files={'content': (artifact.name, f, 'application/octet-stream')},
                auth=(self.user, self.auth),
            )
            import httpx
            try:
                response.raise_for_status()
            except httpx.HTTPStatusError as exc:
                exc.args = (exc.args[0], f"{response.text}")
                raise exc

In the PackageIndex, upload_artifact function. After digging in and getting this error message, I can clearly see what happened here:

<html>
 <head>
  <title>400 This filename has already been used, use a different version. See https://test.pypi.org/help/#file-name-reuse for more information.</title>
 </head>
 <body>
  <h1>400 This filename has already been used, use a different version. See https://test.pypi.org/help/#file-name-reuse for more information.</h1>
  The server could not comply with the request since it is either malformed or otherwise incorrect.<br/><br/>
This filename has already been used, use a different version. See https://test.pypi.org/help/#file-name-reuse for more information.


 </body>
</html>

I am up for contributing a fix, if you're interested in this.

ghost avatar Nov 13 '24 11:11 ghost

Thank you for debugging! I don't quite understand what the error means, can you please explain?

ofek avatar Nov 13 '24 13:11 ofek

I am trying to upload a version name that pypi has already recorded before, and is not a valid upload, but this is just my specific case. These error messages could be useful for all users of hatch. Lmk what you think about the proposed change : )

ghost avatar Nov 13 '24 14:11 ghost

Oh, so that error happens when you try to upload an artifact that already exists? I thought I accounted for that case https://github.com/pypa/hatch/blob/hatch-v1.13.0/src/hatch/publish/index.py#L117

ofek avatar Nov 13 '24 14:11 ofek

That check does not trigger for some reason. But this error message is more general, could be applied for any unaccounted errors like in this case. At least, the users see what's wrong with their config. Now it's a black box: 400 Error. Only jesus knows what happened on pypi side. It's just a matter of parsing the HTTP error text and displaying it in terminal...

ghost avatar Nov 13 '24 14:11 ghost

Yes please feel free to open a PR! I'm just confused about why this particular issue is happening.

ofek avatar Nov 13 '24 16:11 ofek

@ofek could you please look at the suggested change ?

ghost avatar Nov 28 '24 07:11 ghost

Another case where this kind of error shows: attempting to upload a dev version for a package. In the same situation, twine upload --verbose shows the contents of the 400 reply, which is very informative:

INFO     Response from https://test.pypi.org/legacy/:                                                                                                                                         
         400 Bad Request                                                                                                                                                                      
INFO     <html>                                                                                                                                                                               
          <head>                                                                                                                                                                              
           <title>400 The use of local versions in <Version('0.0.3.dev0+ga28325f.d20250630')> is not allowed. See https://packaging.python.org/specifications/core-metadata for more          
         information.</title>                                                                                                                                                                 
          </head>                                                                                                                                                                             
          <body>                                                                                                                                                                              
           <h1>400 The use of local versions in <Version('0.0.3.dev0+ga28325f.d20250630')> is not allowed. See https://packaging.python.org/specifications/core-metadata for more             
         information.</h1>                                                                                                                                                                    
           The server could not comply with the request since it is either malformed or otherwise incorrect.<br/><br/>                                                                        
         The use of local versions in &lt;Version(&#x27;0.0.3.dev0+ga28325f.d20250630&#x27;)&gt; is not allowed. See https://packaging.python.org/specifications/core-metadata for more       
         information.                                                                                                                                                                         
                                                                                                                                                                                              
                                                                                                                                                                                              
          </body>                                                                                                                                                                             
         </html>                                                                                                                                                                              
ERROR    HTTPError: 400 Bad Request from https://test.pypi.org/legacy/                                                                                                                        
         Bad Request                                                                                                                                                                          

ydirson avatar Jun 30 '25 17:06 ydirson

First time hatch user here – I'm trying to publish my very first TestPyPI project using hatch publish -r test, but am seeing 400 Bad Request as discussed here. hatch version is 1.14.1 and hatchling version is 1.27.0. Is this simply insurmountable at this time?

CalvinRusley avatar Oct 03 '25 05:10 CalvinRusley