Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(VS Code) Pylint throws a syntax error when importing a module but works perfectly fine when I run my code

The module I am trying to import is pyinputplus. I do not know why it shows an error if the program imports the module and uses all of its functionality just fine.

This is the error message:

Cannot import 'pyinputplus' due to syntax error 'invalid syntax (<unknown>, line 268)'pylint(syntax-error)

I can do with it because it does not impair functionality but it really annoys me and distracts me when working in my IDE. Plus, it could be a sign that something is not setup right.

like image 848
Marco Tagliani Avatar asked Feb 01 '26 03:02

Marco Tagliani


2 Answers

The problem is the order of the lines in the file.

The type of the function inputStr() is recognized correctly

..... means [snip] of the line(s)

def inputStr(
    prompt="",
    .....):
    # type: (str, .....) -> Any
    """Prompts the user to enter .....
    """

For inputCustom() the type recognition is incorrect because the type line is incorrect

def inputCustom(
    # type: (Callable, str, .....) -> Any
    customValidationFunc,
    prompt="",
    .....):
    """Prompts the user to enter input. ....."""

it should be AFTER the argument list.

def inputCustom(
    customValidationFunc, prompt="", .....):
    # type: (Callable, str, .....) -> Any
    """Prompts the user to enter input. ....."""

Do the same for the function inputNum()

You can edit the __init__.py file and move the 2 comment lines to the correct position.

Then VSC will show the type in the tooltip.

I have created an Issue at pyinputplus for it.


Edit:

I have looked at why pylint shows this cryptic error and the problem is that the Python3.8 compiler reports this as a syntax error when you compile with type_comments=True. Pylint first tries to compile with type_comments=True but does not test the Python3.8 exception correct.

I have written a replacement for astroid/builder.py::_parse_string(). The line is not reported as an error anymore. Maybe when they use the rewrite, they will add an error for a misplaced type annotation.

like image 85
rioV8 Avatar answered Feb 02 '26 15:02

rioV8


I'm the creator of PyInputPlus. Thanks for pointing this out. I've corrected the type hints typo as of PyInputPlus 0.2.11. The solution is to upgrade by running pip install -U pyinputplus.

like image 22
Al Sweigart Avatar answered Feb 02 '26 17:02

Al Sweigart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!