Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: 'String' object does not support item assignment when pyproject.toml is malformed #2198

Closed
lmmx opened this issue Aug 22, 2023 · 2 comments · Fixed by j178/pdm#1
Closed
Labels
🐛 bug Something isn't working

Comments

@lmmx
Copy link

lmmx commented Aug 22, 2023

Steps to reproduce

git clone https://git.launchpad.net/beautifulsoup
cd beautifulsoup
pdm init -v

Actual behavior

louis 🚶 ~/dev/beautifulsoup $ pdm init -v
pyproject.toml already exists, update it now.                                                                                                                                                    
Please enter the Python interpreter to use                                                                                                                                                       
0. /home/louis/miniconda3/bin/python (3.10)                                                                                                                                                      
1. /home/louis/miniconda3/bin/python3.10 (3.10)                                                                                                                                                  
2. /usr/bin/python3.10 (3.10)                                                                                                                                                                    
Please select (0):                                                                                                                                                                               
Is the project a library that is installable?                                                                                                                                                    
If yes, we will need to ask a few more questions to include the project name and build                                                                                                           
backend [y/n] (n): y                                                                                                                                                                             
Project name (beautifulsoup):                                                                                                                                                                    
Project version (0.1.0):                                                                                                                                                                         
Project description ():                                                                                                                                                                          
Which build backend to use?                                                                                                                                                                      
0. pdm-backend                                                                                                                                                                                   
1. setuptools                                                                                                                                                                                    
2. flit-core                                                                                                                                                                                     
3. hatchling                                                                                                                                                                                     
4. pdm-pep517                                                                                                                                                                                    
Please select (0):                                                                                                                                                                               
License(SPDX name) (MIT):                                                                                                                                                                        
Author name (Louis Maddox):                                                                                                                                                                      
Author email (louismmx@gmail.com):                                                                                                                                                               
Python requires('*' to allow any) (>=3.10):                                                                                                                                                      
Traceback (most recent call last):                                                                                                                                                               
  File "/home/louis/miniconda3/bin/pdm", line 8, in <module>                                                                                                                                     
    sys.exit(main())                                                                                                                                                                             
  File "/home/louis/miniconda3/lib/python3.10/site-packages/pdm/core.py", line 290, in main                                                                                                      
    return Core().main(args or sys.argv[1:])                                                                                                                                                     
  File "/home/louis/miniconda3/lib/python3.10/site-packages/pdm/core.py", line 208, in main                                                                                                      
    raise cast(Exception, err).with_traceback(traceback) from None                                                                                                                               
  File "/home/louis/miniconda3/lib/python3.10/site-packages/pdm/core.py", line 203, in main                                                                                                      
    self.handle(project, options)                                                                                                                                                                
  File "/home/louis/miniconda3/lib/python3.10/site-packages/pdm/core.py", line 157, in handle                                                                                                    
    command.handle(project, options)                                                                                                                                                             
  File "/home/louis/miniconda3/lib/python3.10/site-packages/pdm/cli/commands/init.py", line 243, in handle                                                                                       
    self.do_init(project, options=options)                                                                                                                                                       
  File "/home/louis/miniconda3/lib/python3.10/site-packages/pdm/cli/commands/init.py", line 38, in do_init                                                                                       
    self._init_builtin(project, options)                                                                                                                                                         
  File "/home/louis/miniconda3/lib/python3.10/site-packages/pdm/cli/commands/init.py", line 79, in _init_builtin                                                                                 
    template.generate(project.root, metadata)                                                                                                                                                    
  File "/home/louis/miniconda3/lib/python3.10/site-packages/pdm/cli/templates/__init__.py", line 78, in generate                                                                                 
    self._generate_pyproject(target_path / "pyproject.toml", metadata)                                                                                                                           
  File "/home/louis/miniconda3/lib/python3.10/site-packages/pdm/cli/templates/__init__.py", line 130, in _generate_pyproject                                                                     
    merge_dictionary(content, template_content)                                                                                                                                                  
  File "/home/louis/miniconda3/lib/python3.10/site-packages/pdm/cli/utils.py", line 644, in merge_dictionary                                                                                     
    merge_dictionary(target[key], value, append_array=append_array)                                                                                                                              
  File "/home/louis/miniconda3/lib/python3.10/site-packages/pdm/cli/utils.py", line 644, in merge_dictionary                                                                                     
    merge_dictionary(target[key], value, append_array=append_array)                                                                                                                              
  File "/home/louis/miniconda3/lib/python3.10/site-packages/pdm/cli/utils.py", line 642, in merge_dictionary                                                                                     
    target[key] = value                                                                                                                                                                          
TypeError: 'String' object does not support item assignment 

Expected behavior

Environment Information

PDM version:
  2.8.2
Python Interpreter:
  /home/louis/miniconda3/bin/python (3.10)
Project Root:
  /home/louis/dev/beautifulsoup
Local Packages:
  
{
  "implementation_name": "cpython",
  "implementation_version": "3.10.6",
  "os_name": "posix",
  "platform_machine": "x86_64",
  "platform_release": "5.15.0-43-generic",
  "platform_system": "Linux",
  "platform_version": "#46-Ubuntu SMP Tue Jul 12 10:30:17 UTC 2022",
  "python_full_version": "3.10.6",
  "platform_python_implementation": "CPython",
  "python_version": "3.10",
  "sys_platform": "linux"
}

I inspected with a breakpoint at the error site at the end of the traceback and can see target is being assigned as "MIT" hence the error.

The type annotation is for a Mapping[str,str] so something has gone wrong.

This library is using a bad license value in its pyproject.toml (just a str not a dict type). I thought you might want to know that it crashes pdm init though and maybe pdm would be better exitting with a message about what failed to validate.

@lmmx lmmx added the 🐛 bug Something isn't working label Aug 22, 2023
@frostming
Copy link
Collaborator

Exact same steps and I cannot reproduce, what is it version of tomlkit are you using? Run pdm self list|grep tomlkit to see it.

@lmmx
Copy link
Author

lmmx commented Aug 29, 2023

Huh, weird! Latest version

tomlkit                       0.12.1      Style preserving TOML library                   

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants