CITGuru / PyInquirer

A Python module for common interactive command line user interfaces
MIT License
1.92k stars 236 forks source link

PyInquirer\prompts\input.py", line 20 TypeError: issubclass() arg 1 must be a class #19

Closed vc1 closed 5 years ago

vc1 commented 5 years ago
Traceback (most recent call last):
  File "main.py", line 8, in <module>
    answers = choice()
  File "C:\Users\x\choice.py", line 85, in choice
    answers = Dict(prompt(questions, style=style))
  File "D:\Program Files\Python37\lib\site-packages\PyInquirer\prompt.py", line 63, in prompt
    application = getattr(prompts, type).question(message, **_kwargs)
  File "D:\Program Files\Python37\lib\site-packages\PyInquirer\prompts\input.py", line 20, in question
    if issubclass(validate_prompt, Validator):
  File "D:\Program Files\Python37\lib\abc.py", line 143, in __subclasscheck__
    return _abc_subclasscheck(cls, subclass)
TypeError: issubclass() arg 1 must be a class
vc1 commented 5 years ago
def question(message, **kwargs):
    default = kwargs.pop('default', '')
    validate_prompt = kwargs.pop('validate', None)
    if validate_prompt:
        if callable(validate_prompt):
            class _InputValidator(Validator):
                def validate(self, document):
                    verdict = validate_prompt(document.text)
                    if not verdict == True:
                        if verdict == False:
                            verdict = 'invalid input'
                        raise ValidationError(
                            message=verdict,
                            cursor_position=len(document.text))
            kwargs['validator'] = _InputValidator()
        elif issubclass(validate_prompt, Validator):
            kwargs['validator'] = validate_prompt()

check callable first, then issubclass

CITGuru commented 5 years ago

Hello @vc1, sorry for late reply and thanks for pointing this out. Can you make this a PR instead?

davidlj95 commented 5 years ago
def question(message, **kwargs):
    default = kwargs.pop('default', '')
    validate_prompt = kwargs.pop('validate', None)
    if validate_prompt:
        if callable(validate_prompt):
            class _InputValidator(Validator):
                def validate(self, document):
                    verdict = validate_prompt(document.text)
                    if not verdict == True:
                        if verdict == False:
                            verdict = 'invalid input'
                        raise ValidationError(
                            message=verdict,
                            cursor_position=len(document.text))
            kwargs['validator'] = _InputValidator()
        elif issubclass(validate_prompt, Validator):
            kwargs['validator'] = validate_prompt()

check callable first, then issubclass

This does not work, as the Validator class is callable and it is not desired to enter the first if clause

CITGuru commented 5 years ago

The issue has been fixed and the fix is merged

Psycoguana commented 4 years ago

The issue has been fixed and the fix is merged

Hello, I'm having this exact same problem when trying to run the example code