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

Crash when a test function is decorated with @pytest.fixture #4612

Closed
paulalex opened this issue Jun 23, 2021 · 12 comments · Fixed by #4613
Closed

Crash when a test function is decorated with @pytest.fixture #4612

paulalex opened this issue Jun 23, 2021 · 12 comments · Fixed by #4613
Labels
Blocker 🙅 Blocks the next release Crash 💥 A bug that makes pylint crash
Milestone

Comments

@paulalex
Copy link

paulalex commented Jun 23, 2021

Question

I am using pylint 2.8.3 and one of my repos pipeline fails for the following exception within pylint:

i is not None and i.qname() in qnames or i.name in qnames

 TypeError: 'in <string>' requires string as left operand, not Uninferable

I cannot work out what is causing this but if I remove a failing module then this exception just gets thrown in another module.

Can anyone assist me with debugging this issue?

@Pierre-Sassoulas
Copy link
Member

Hello, sure. It look like an unhandled inference fail on astroid. Could you provide the code causing this exception, please ? (You can follow the template for a 'bug' issue)

@paulalex
Copy link
Author

The problem is that it falls over for any of the modules and I cannot see what is causing it., for example of I had module A, B, C in this repo and it currently fails on A, if I then comment out A it fails on B and so on.

Is there some way I can further debug because this has only just started happening and the last build against master was fine, I created a new PR against master with a readme only change and it still fails...

The previous change was also not even a python code change but a terraform only change.

@Pierre-Sassoulas
Copy link
Member

I would need the traceback or the code it fails on to help you. It probably has something to do with a decorator, you could try isolating them. Maybe you can create a minimal sharable example that way.

@paulalex
Copy link
Author

paulalex commented Jun 23, 2021

Here is the stack trace in the build logs:

Exception on node <Call l.92 at 0x7f9cb66bc220> in file '/codebuild/output/src662162499/src/ind-openit-export-forecasting-service/src/test/unit/export_forecasting_api_tests.py'
--
462 | Traceback (most recent call last):
463 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-uUHobIbO/bin/pylint", line 8, in <module>
464 | sys.exit(run_pylint())
465 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-uUHobIbO/lib/python3.8/site-packages/pylint/__init__.py", line 24, in run_pylint
466 | PylintRun(sys.argv[1:])
467 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-uUHobIbO/lib/python3.8/site-packages/pylint/lint/run.py", line 384, in __init__
468 | linter.check(args)
469 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-uUHobIbO/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 962, in check
470 | self._check_files(
471 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-uUHobIbO/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 996, in _check_files
472 | self._check_file(get_ast, check_astroid_module, name, filepath, modname)
473 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-uUHobIbO/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1022, in _check_file
474 | check_astroid_module(ast_node)
475 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-uUHobIbO/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1159, in check_astroid_module
476 | retval = self._check_astroid_module(
477 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-uUHobIbO/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1204, in _check_astroid_module
478 | walker.walk(ast_node)
479 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-uUHobIbO/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 77, in walk
480 | self.walk(child)
481 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-uUHobIbO/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 77, in walk
482 | self.walk(child)
483 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-uUHobIbO/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 77, in walk
484 | self.walk(child)
485 | [Previous line repeated 2 more times]
486 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-uUHobIbO/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 74, in walk
487 | callback(astroid)
488 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-uUHobIbO/lib/python3.8/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 885, in visit_call
489 | self._check_consider_using_with(node)
490 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-uUHobIbO/lib/python3.8/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 1302, in _check_consider_using_with
491 | if could_be_used_in_with and not _is_inside_context_manager(node):
492 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-uUHobIbO/lib/python3.8/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 112, in _is_inside_context_manager
493 | return frame.name == "__enter__" or utils.decorated_with(
494 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-uUHobIbO/lib/python3.8/site-packages/pylint/checkers/utils.py", line 846, in decorated_with
495 | if any(
496 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-uUHobIbO/lib/python3.8/site-packages/pylint/checkers/utils.py", line 847, in <genexpr>
497 | i is not None and i.qname() in qnames or i.name in qnames
498 | TypeError: 'in <string>' requires string as left operand, not Uninferable
499 | make: *** [Makefile:33: lint] Error 1

@Pierre-Sassoulas
Copy link
Member

Is there a decorator at node <Call l.92 at 0x7f9cb66bc220> in file '/codebuild/output/src662162499/src/ind-openit-export-forecasting-service/src/test/unit/export_forecasting_api_tests.py' ? Can you test the result of pylint on this function and its import in a single file ?

@paulalex
Copy link
Author

The only decorator defined in that file is @pytest.fixture and this is used multiple times.

@paulalex
Copy link
Author

If I comment out everything but the first fixture in the test file it now moves on and fails on the next test file:

Exception on node <Call l.66 at 0x7fa704e89100> in file '/codebuild/output/src534352411/src//ind-openit-export-forecasting-service/src/test/unit/export_forecasting_controller_tests.py'
568 | Traceback (most recent call last):
569 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-T348z7Ej/bin/pylint", line 8, in <module>
570 | sys.exit(run_pylint())
571 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-T348z7Ej/lib/python3.8/site-packages/pylint/__init__.py", line 24, in run_pylint
572 | PylintRun(sys.argv[1:])
573 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-T348z7Ej/lib/python3.8/site-packages/pylint/lint/run.py", line 384, in __init__
574 | linter.check(args)
575 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-T348z7Ej/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 962, in check
576 | self._check_files(
577 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-T348z7Ej/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 996, in _check_files
578 | self._check_file(get_ast, check_astroid_module, name, filepath, modname)
579 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-T348z7Ej/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1022, in _check_file
580 | check_astroid_module(ast_node)
581 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-T348z7Ej/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1159, in check_astroid_module
582 | retval = self._check_astroid_module(
583 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-T348z7Ej/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1204, in _check_astroid_module
584 | walker.walk(ast_node)
585 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-T348z7Ej/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 77, in walk
586 | self.walk(child)
587 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-T348z7Ej/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 77, in walk
588 | self.walk(child)
589 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-T348z7Ej/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 77, in walk
590 | self.walk(child)
591 | [Previous line repeated 2 more times]
592 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-T348z7Ej/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 74, in walk
593 | callback(astroid)
594 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-T348z7Ej/lib/python3.8/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 885, in visit_call
595 | self._check_consider_using_with(node)
596 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-T348z7Ej/lib/python3.8/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 1302, in _check_consider_using_with
597 | if could_be_used_in_with and not _is_inside_context_manager(node):
598 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-T348z7Ej/lib/python3.8/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 112, in _is_inside_context_manager
599 | return frame.name == "__enter__" or utils.decorated_with(
600 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-T348z7Ej/lib/python3.8/site-packages/pylint/checkers/utils.py", line 846, in decorated_with
601 | if any(
602 | File "/root/.local/share/virtualenvs/ind-openit-export-forecasting-service-T348z7Ej/lib/python3.8/site-packages/pylint/checkers/utils.py", line 847, in <genexpr>
603 | i is not None and i.qname() in qnames or i.name in qnames
604 | TypeError: 'in <string>' requires string as left operand, not Uninferable
605 | make: *** [Makefile:33: lint] Error 1
606


@Pierre-Sassoulas
Copy link
Member

Is it possible to see line 92 previously or line 66 now ?

@paulalex
Copy link
Author

paulalex commented Jun 23, 2021

Sure:

@pytest.fixture
def qm_file():
   qm_file = open("src/test/resources/example_qm_file.csv").read() << this is line 66

   return qm_file

@Pierre-Sassoulas Pierre-Sassoulas added Blocker 🙅 Blocks the next release Crash 💥 A bug that makes pylint crash and removed Question labels Jun 23, 2021
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.9.0 milestone Jun 23, 2021
Pierre-Sassoulas added a commit to Pierre-Sassoulas/pylint that referenced this issue Jun 23, 2021
@paulalex
Copy link
Author

paulalex commented Jun 23, 2021

Hi @Pierre-Sassoulas,

Thanks!!

You have helped me to diagnose the issue, I have updated the PR to use a context manager for all calls to open() and this has resolved the issue.

This might also help you diagnose the issue (if you still think there is one), for example the fixtures in the tests now look like:

@pytest.fixture
def qm_file():

    with open("src/test/resources/example_qm_file.csv") as qm_file_in:
        qm_file = qm_file_in.read()

    return qm_file

I am currently replacing the calls which open the test resource files in fixtures in this feature branch and it seems to get further and further, I will feedback if this turns out to be a red herring but it certainly looks to be resolving the issue.

Pierre-Sassoulas added a commit to Pierre-Sassoulas/pylint that referenced this issue Jun 23, 2021
And astroid can't infer the name of the decorator.

Closes pylint-dev#4612
@Pierre-Sassoulas Pierre-Sassoulas changed the title Issue with single repo Crash when a test function is decorated with @pytest.fixture Jun 23, 2021
@Pierre-Sassoulas
Copy link
Member

The fix will be available in 2.9.0, thank you for signaling this bug and taking the time to pin down the problem 😄

@paulalex
Copy link
Author

Awesome thanks, and thanks for maintaining such a useful tool!

Pierre-Sassoulas added a commit to Pierre-Sassoulas/pylint that referenced this issue Jun 23, 2021
And astroid can't infer the name of the decorator when using ``open``
without ``with``.

Closes pylint-dev#4612
Pierre-Sassoulas added a commit that referenced this issue Jun 23, 2021
#4613)

And astroid can't infer the name of the decorator when using ``open``
without ``with``.

Closes #4612
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blocker 🙅 Blocks the next release Crash 💥 A bug that makes pylint crash
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants