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

poetry init - Pressing <enter> at "Enter package # to add ..." causes AttributeError: 'NoneType' object has no attribute 'replace' #2355

Closed
3 tasks done
asottile opened this issue Apr 26, 2020 · 4 comments · Fixed by #4606
Labels
area/cli Related to the command line area/error-handling Bad error messages/insufficient error handling area/init Related to 'poetry init'/project creation area/ux Features and improvements related to the user experience

Comments

@asottile
Copy link

  • I am on the latest Poetry version.
  • I have searched the issues of this repo and believe that this is not a duplicate.
  • If an exception occurs when executing a command, I executed it again in debug mode (-vvv option). (it made no difference in the output)
  • OS version and name: ubuntu 18.04
  • Poetry version: 1.0.5
  • Link of a Gist with the contents of your pyproject.toml file: N/A

Issue

$ poetry init

This command will guide you through creating your pyproject.toml config.

Package name [y]:  
Version [0.1.0]:  
Description []:  
Author [Anthony Sottile <[email protected]>, n to skip]:  
License []:  
Compatible Python versions [^3.6]:  

Would you like to define your main dependencies interactively? (yes/no) [yes] 
You can specify a package in the following forms:
  - A single name (requests)
  - A name and a constraint (requests ^2.23.0)
  - A git url (git+https://github.com/python-poetry/poetry.git)
  - A git url with a revision (git+https://github.com/python-poetry/poetry.git#develop)
  - A file path (../my-package/my-package.whl)
  - A directory (../my-package/)
  - An url (https://example.com/packages/my-package-0.1.0.tar.gz)

Search for package to add (or leave blank to continue): 

Would you like to define your development dependencies interactively? (yes/no) [yes] 
Search for package to add (or leave blank to continue): astpretty
Found 3 packages matching astpretty

Enter package # to add, or the complete package name if it is not listed: 
 [0] astpretty
 [1] asteria
 [2] moshmosh-base
 > 
'NoneType' object has no attribute 'replace'

Enter package # to add, or the complete package name if it is not listed: 
 [0] astpretty
 [1] asteria
 [2] moshmosh-base
 > 

very minor, but I expect a human-readable error message instead, or to default to the first one (it was an exact name match after all!)

@asottile asottile added kind/bug Something isn't working as expected status/triage This issue needs to be triaged labels Apr 26, 2020
@finswimmer finswimmer added area/cli Related to the command line area/error-handling Bad error messages/insufficient error handling labels Apr 28, 2020
@finswimmer finswimmer added Good First Issue area/ux Features and improvements related to the user experience and removed status/triage This issue needs to be triaged kind/bug Something isn't working as expected labels Aug 2, 2021
@doctaphred
Copy link

Additional occurrence of the issue, with a stack trace (via poetry init -vvv):

Would you like to define your development dependencies interactively? (yes/no) [yes]
You can specify a package in the following forms:
  - A single name (requests)
  - A name and a constraint (requests@^2.23.0)
  - A git url (git+https://github.com/python-poetry/poetry.git)
  - A git url with a revision (git+https://github.com/python-poetry/poetry.git#develop)
  - A file path (../my-package/my-package.whl)
  - A directory (../my-package/)
  - A url (https://example.com/packages/my-package-0.1.0.tar.gz)

Search for package to add (or leave blank to continue): IPython
Found 20 packages matching IPython

Enter package # to add, or the complete package name if it is not listed:
 [0] twisted-ipython
 [1] ipython-elasticsearch
 [2] ipython-bg
 [3] mypyc-ipython
 [4] ipython-agnoster
 [5] ipython-futhark
 [6] ipython-sparksql
 [7] progressbar-ipython
 [8] ipython-autotime
 [9] ipython-gremlin
 > IPython
Value "IPython" is invalid

Enter package # to add, or the complete package name if it is not listed:
 [0] twisted-ipython
 [1] ipython-elasticsearch
 [2] ipython-bg
 [3] mypyc-ipython
 [4] ipython-agnoster
 [5] ipython-futhark
 [6] ipython-sparksql
 [7] progressbar-ipython
 [8] ipython-autotime
 [9] ipython-gremlin
 >
'NoneType' object has no attribute 'replace'

Enter package # to add, or the complete package name if it is not listed:
 [0] twisted-ipython
 [1] ipython-elasticsearch
 [2] ipython-bg
 [3] mypyc-ipython
 [4] ipython-agnoster
 [5] ipython-futhark
 [6] ipython-sparksql
 [7] progressbar-ipython
 [8] ipython-autotime
 [9] ipython-gremlin
 >

  Stack trace:

  11  ~/venvs/default/lib/python3.9/site-packages/clikit/console_application.py:131 in run
       129│             parsed_args = resolved_command.args
       130│
     → 131│             status_code = command.handle(parsed_args, io)
       132│         except KeyboardInterrupt:
       133│             status_code = 1

  10  ~/venvs/default/lib/python3.9/site-packages/clikit/api/command/command.py:120 in handle
       118│     def handle(self, args, io):  # type: (Args, IO) -> int
       119│         try:
     → 120│             status_code = self._do_handle(args, io)
       121│         except KeyboardInterrupt:
       122│             if io.is_debug():

   9  ~/venvs/default/lib/python3.9/site-packages/clikit/api/command/command.py:171 in _do_handle
       169│         handler_method = self._config.handler_method
       170│
     → 171│         return getattr(handler, handler_method)(args, io, self)
       172│
       173│     def __repr__(self):  # type: () -> str

   8  ~/venvs/default/lib/python3.9/site-packages/cleo/commands/command.py:92 in wrap_handle
        90│         self._command = command
        91│
     →  92│         return self.handle()
        93│
        94│     def handle(self):  # type: () -> Optional[int]

   7  ~/venvs/default/lib/python3.9/site-packages/poetry/console/commands/init.py:198 in handle
       196│
       197│             dev_requirements.update(
     → 198│                 self._format_requirements(self._determine_requirements([]))
       199│             )
       200│             self.line("")

   6  ~/venvs/default/lib/python3.9/site-packages/poetry/console/commands/init.py:279 in _determine_requirements
       277│                     )
       278│
     → 279│                     package = self.choice(
       280│                         "\nEnter package # to add, or the complete package name if it is not listed",
       281│                         choices,

   5  ~/venvs/default/lib/python3.9/site-packages/cleo/commands/command.py:172 in choice
       170│         question.set_multi_select(multiple)
       171│
     → 172│         return self._io.ask_question(question)
       173│
       174│     def create_question(self, question, type=None, **kwargs):

   4  ~/venvs/default/lib/python3.9/site-packages/cleo/io/io_mixin.py:50 in ask_question
        48│         Asks a question.
        49│         """
     →  50│         answer = question.ask(self)
        51│
        52│         return answer

   3  ~/venvs/default/lib/python3.9/site-packages/clikit/ui/components/question.py:80 in ask
        78│         interviewer = lambda: self._do_ask(io)
        79│
     →  80│         return self._validate_attempts(interviewer, io)
        81│
        82│     def _do_ask(self, io):  # type: (IO) -> str

   2  ~/venvs/default/lib/python3.9/site-packages/clikit/ui/components/question.py:254 in _validate_attempts
       252│                 attempts -= 1
       253│
     → 254│         raise error
       255│
       256│     def _read_from_input(self, io):

   1  ~/venvs/default/lib/python3.9/site-packages/clikit/ui/components/question.py:247 in _validate_attempts
       245│
       246│             try:
     → 247│                 return self._validator(interviewer())
       248│             except Exception as e:
       249│                 error = e

  AttributeError

  'NoneType' object has no attribute 'replace'

  at ~/venvs/default/lib/python3.9/site-packages/clikit/ui/components/choice_question.py:23 in validate
       19│         # Collapse all spaces.
       20│         if isinstance(selected, int):
       21│             selected = str(selected)
       22│
    →  23│         selected_choices = selected.replace(" ", "")
       24│
       25│         if self._question.supports_multiple_choices():
       26│             # Check for a separated comma values
       27│             if not re.match("^[a-zA-Z0-9_-]+(?:,[a-zA-Z0-9_-]+)*$", selected_choices):

Python 3.9.0 + Poetry 1.1.8 on macOS.

Pretty sure this just needs a default value of "" instead of None somewhere along that stack trace.

jacobperron added a commit to jacobperron/poetry that referenced this issue Oct 7, 2021
Fixes python-poetry#2355

This works around an issue with cleo's `Command.choice` implementation.
If the user does not make a selection then an AttributeError is raised.
jacobperron added a commit to jacobperron/poetry that referenced this issue Oct 7, 2021
Fixes python-poetry#2355

This works around an issue with cleo's `Command.choice` implementation.
If the user does not make a selection then an AttributeError is raised.
@jacobperron
Copy link
Contributor

jacobperron commented Oct 7, 2021

I think this issues comes from cleo's ChoiceQuestion not supporting an empty choice. IMO, it makes sense to update ChoiceQuestion upstream, but I've proposed a workaround here #4606

jacobperron added a commit to jacobperron/cleo that referenced this issue Oct 7, 2021
Otherwise, an AttributeError is raised.

See related downstream ticket: python-poetry/poetry#2355

Signed-off-by: Jacob Perron <[email protected]>
@jacobperron
Copy link
Contributor

I've also opened a related patch upstream: python-poetry/cleo#109

@finswimmer finswimmer added the area/init Related to 'poetry init'/project creation label Mar 31, 2022
jacobperron added a commit to jacobperron/poetry that referenced this issue May 19, 2022
Fixes python-poetry#2355

This works around an issue with cleo's `Command.choice` implementation.
If the user does not make a selection then an AttributeError is raised.
@abn abn closed this as completed in #4606 May 20, 2022
abn pushed a commit that referenced this issue May 20, 2022
Fixes #2355

This works around an issue with cleo's `Command.choice` implementation.
If the user does not make a selection then an AttributeError is raised.
Copy link

github-actions bot commented Mar 2, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/cli Related to the command line area/error-handling Bad error messages/insufficient error handling area/init Related to 'poetry init'/project creation area/ux Features and improvements related to the user experience
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants