pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.31k stars 1.13k forks source link

importerror due to cwd being part of sys.path #2969

Closed twmr closed 4 years ago

twmr commented 5 years ago

Steps to reproduce

> $ python3 -m venv pylint_venv                                                                                                                               

> $ act pylint_venv                                                                                                                                           
activate virtualenv in pylint_venv

(pylint_venv) > $ pip install toml pylint                                                                                                                     
Collecting toml
  Using cached https://files.pythonhosted.org/packages/a2/12/ced7105d2de62fa7c8fb5fce92cc4ce66b57c95fb875e9318dba7f8c5db0/toml-0.10.0-py2.py3-none-any.whl
Collecting pylint
  Using cached https://files.pythonhosted.org/packages/60/c2/b3f73f4ac008bef6e75bca4992f3963b3f85942e0277237721ef1c151f0d/pylint-2.3.1-py3-none-any.whl
Collecting mccabe<0.7,>=0.6 (from pylint)
  Using cached https://files.pythonhosted.org/packages/87/89/479dc97e18549e21354893e4ee4ef36db1d237534982482c3681ee6e7b57/mccabe-0.6.1-py2.py3-none-any.whl
Collecting isort<5,>=4.2.5 (from pylint)
  Cache entry deserialization failed, entry ignored
  Using cached https://files.pythonhosted.org/packages/1c/d9/bf5848b376e441ff358a14b954476423eeeb8c9b78c10074b7f53ce2918d/isort-4.3.20-py2.py3-none-any.whl
Collecting astroid<3,>=2.2.0 (from pylint)
  Using cached https://files.pythonhosted.org/packages/d5/ad/7221a62a2dbce5c3b8c57fd18e1052c7331adc19b3f27f1561aa6e620db2/astroid-2.2.5-py3-none-any.whl
Collecting wrapt (from astroid<3,>=2.2.0->pylint)
  Using cached https://files.pythonhosted.org/packages/23/84/323c2415280bc4fc880ac5050dddfb3c8062c2552b34c2e512eb4aa68f79/wrapt-1.11.2.tar.gz
Collecting six (from astroid<3,>=2.2.0->pylint)
  Using cached https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
Collecting typed-ast>=1.3.0; implementation_name == "cpython" (from astroid<3,>=2.2.0->pylint)
  Using cached https://files.pythonhosted.org/packages/fb/56/dd4e168a0009da85c78c6cfe91f5b2df2c7bbed60f3ba778c4a71289e6fb/typed_ast-1.4.0-cp37-cp37m-manylinux1_x86_64.whl
Collecting lazy-object-proxy (from astroid<3,>=2.2.0->pylint)
  Using cached https://files.pythonhosted.org/packages/43/a5/1b19b094ad19bce55b5b6d434020f5537b424fd2b3cff0fbef23d7bb5a95/lazy_object_proxy-1.4.1-cp37-cp37m-manylinux1_x86_64.whl
Building wheels for collected packages: wrapt
  Running setup.py bdist_wheel for wrapt ... error
  Complete output from command /tmp/pylint_venv/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-1jfcn_y2/wrapt/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/pip-wheel-_5u2x0xx --python-tag cp37:
  usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
     or: -c --help [cmd1 cmd2 ...]
     or: -c --help-commands
     or: -c cmd --help

  error: invalid command 'bdist_wheel'

  ----------------------------------------
  Failed building wheel for wrapt
  Running setup.py clean for wrapt
Failed to build wrapt
Installing collected packages: toml, mccabe, isort, wrapt, six, typed-ast, lazy-object-proxy, astroid, pylint
  Running setup.py install for wrapt ... done
Successfully installed astroid-2.2.5 isort-4.3.20 lazy-object-proxy-1.4.1 mccabe-0.6.1 pylint-2.3.1 six-1.12.0 toml-0.10.0 typed-ast-1.4.0 wrapt-1.11.2

(pylint_venv) > $ cd ~/gitrepos/pytest/src/_pytest                                                                                                            

(pylint_venv) > $ pylint logging.py                                                                       
************* Module _pytest.logging
logging.py:85:2: W0511: TODO optimize this by introducing an option that tells the (fixme)
logging.py:265:0: C0301: Line too long (106/100) (line-too-long)
logging.py:303:0: C0301: Line too long (118/100) (line-too-long)
logging.py:305:0: C0301: Line too long (111/100) (line-too-long)
logging.py:308:0: C0301: Line too long (117/100) (line-too-long)
logging.py:320:0: C0301: Line too long (111/100) (line-too-long)
logging.py:324:0: C0301: Line too long (106/100) (line-too-long)
logging.py:327:0: C0301: Line too long (110/100) (line-too-long)
logging.py:338:0: C0301: Line too long (111/100) (line-too-long)
logging.py:342:0: C0301: Line too long (106/100) (line-too-long)
logging.py:451:0: C0330: Wrong hanging indentation before block (add 4 spaces).
            log_format
            ^   | (bad-continuation)
logging.py:538:0: C0330: Wrong hanging indentation before block (add 4 spaces).
            LogCaptureHandler(), formatter=self.formatter, level=self.log_level
            ^   | (bad-continuation)
logging.py:603:0: C0330: Wrong hanging indentation before block (add 4 spaces).
                        self.log_file_handler, level=self.log_file_level
                        ^   | (bad-continuation)
logging.py:646:0: C0301: Line too long (111/100) (line-too-long)
logging.py:649:0: C0301: Line too long (110/100) (line-too-long)
logging.py:6:0: E0401: Unable to import 'py' (import-error)
logging.py:22:28: E1101: Module 'logging' has no 'Formatter' member (no-member)
logging.py:28:8: E1101: Module 'logging' has no 'CRITICAL' member (no-member)
logging.py:29:8: E1101: Module 'logging' has no 'ERROR' member (no-member)
logging.py:30:8: E1101: Module 'logging' has no 'WARNING' member (no-member)
logging.py:31:8: E1101: Module 'logging' has no 'WARN' member (no-member)
logging.py:32:8: E1101: Module 'logging' has no 'INFO' member (no-member)
logging.py:33:8: E1101: Module 'logging' has no 'DEBUG' member (no-member)
logging.py:34:8: E1101: Module 'logging' has no 'NOTSET' member (no-member)
logging.py:40:29: W0212: Access to a protected member _fmt of a client class (protected-access)
logging.py:50:29: E1101: Module 'logging' has no 'getLevelName' member (no-member)
logging.py:62:4: C0111: Missing method docstring (missing-docstring)
logging.py:64:8: W0212: Access to a protected member _fmt of a client class (protected-access)
logging.py:22:0: R0903: Too few public methods (1/2) (too-few-public-methods)
logging.py:459:8: W0201: Attribute '_style' defined outside __init__ (attribute-defined-outside-init)
logging.py:68:28: E1101: Module 'logging' has no 'PercentStyle' member (no-member)
logging.py:81:4: C0111: Missing method docstring (missing-docstring)
logging.py:82:8: R1705: Unnecessary "else" after "return" (no-else-return)
logging.py:68:0: R0903: Too few public methods (1/2) (too-few-public-methods)
logging.py:95:0: C0111: Missing function docstring (missing-docstring)
logging.py:95:0: R1710: Either all return statements in a function should return an expression, or none of them should. (inconsistent-return-statements)
logging.py:108:51: W0622: Redefining built-in 'type' (redefined-builtin)
logging.py:191:18: E1101: Module 'logging' has no 'getLogger' member (no-member)
logging.py:216:24: E1101: Module 'logging' has no 'StreamHandler' member (no-member)
logging.py:221:8: E1101: Module 'logging' has no 'StreamHandler' member (no-member)
logging.py:227:8: E1101: Module 'logging' has no 'StreamHandler' member (no-member)
logging.py:229:4: C0111: Missing method docstring (missing-docstring)
logging.py:231:8: W0201: Attribute 'stream' defined outside __init__ (attribute-defined-outside-init)
logging.py:250:21: E1101: Module 'logging' has no 'getLogger' member (no-member)
logging.py:273:8: R1705: Unnecessary "else" after "return" (no-else-return)
logging.py:331:17: E1101: Module 'logging' has no 'getLogger' member (no-member)
logging.py:344:17: E1101: Module 'logging' has no 'getLogger' member (no-member)
logging.py:337:4: R0201: Method could be a function (no-self-use)
logging.py:366:4: W0212: Access to a protected member _finalize of a client class (protected-access)
logging.py:390:59: W0631: Using possibly undefined loop variable 'setting_name' (undefined-loop-variable)
logging.py:369:0: R1710: Either all return statements in a function should return an expression, or none of them should. (inconsistent-return-statements)
logging.py:396:0: C0111: Missing function docstring (missing-docstring)
logging.py:400:0: R0902: Too many instance attributes (11/7) (too-many-instance-attributes)
logging.py:424:34: E1101: Module 'logging' has no 'Formatter' member (no-member)
logging.py:430:36: E1101: Module 'logging' has no 'FileHandler' member (no-member)
logging.py:439:33: W0108: Lambda may not be necessary (unnecessary-lambda)
logging.py:457:24: E1101: Module 'logging' has no 'Formatter' member (no-member)
logging.py:459:8: W0212: Access to a protected member _style of a client class (protected-access)
logging.py:459:49: W0212: Access to a protected member _fmt of a client class (protected-access)
logging.py:459:49: W0212: Access to a protected member _style of a client class (protected-access)
logging.py:500:32: E1101: Module 'logging' has no 'FileHandler' member (no-member)
logging.py:514:4: C0111: Missing method docstring (missing-docstring)
logging.py:564:4: C0111: Missing method docstring (missing-docstring)
logging.py:569:4: C0111: Missing method docstring (missing-docstring)
logging.py:574:4: C0111: Missing method docstring (missing-docstring)
logging.py:579:4: C0111: Missing method docstring (missing-docstring)
logging.py:586:4: C0111: Missing method docstring (missing-docstring)
logging.py:591:4: C0111: Missing method docstring (missing-docstring)
logging.py:596:4: C0111: Missing method docstring (missing-docstring)
logging.py:614:4: C0111: Missing method docstring (missing-docstring)
logging.py:644:32: E1101: Module 'logging' has no 'StreamHandler' member (no-member)
logging.py:658:8: E1101: Module 'logging' has no 'StreamHandler' member (no-member)
logging.py:675:4: C0111: Missing method docstring (missing-docstring)
logging.py:692:12: E1101: Module 'logging' has no 'StreamHandler' member (no-member)
logging.py:684:16: W0201: Attribute '_first_record_emitted' defined outside __init__ (attribute-defined-outside-init)
logging.py:691:16: W0201: Attribute '_section_name_shown' defined outside __init__ (attribute-defined-outside-init)
Traceback (most recent call last):
  File "/tmp/pylint_venv/bin/pylint", line 10, in <module>
    sys.exit(run_pylint())
  File "/tmp/pylint_venv/lib/python3.7/site-packages/pylint/__init__.py", line 20, in run_pylint
    Run(sys.argv[1:])
  File "/tmp/pylint_venv/lib/python3.7/site-packages/pylint/lint.py", line 1628, in __init__
    linter.check(args)
  File "/tmp/pylint_venv/lib/python3.7/site-packages/pylint/lint.py", line 943, in check
    self._do_check(files_or_modules)
  File "/tmp/pylint_venv/lib/python3.7/site-packages/pylint/lint.py", line 1075, in _do_check
    self.check_astroid_module(ast_node, walker, rawcheckers, tokencheckers)
  File "/tmp/pylint_venv/lib/python3.7/site-packages/pylint/lint.py", line 1158, in check_astroid_module
    walker.walk(ast_node)
  File "/tmp/pylint_venv/lib/python3.7/site-packages/pylint/utils.py", line 1305, in walk
    cb(astroid)
  File "/tmp/pylint_venv/lib/python3.7/site-packages/pylint/checkers/imports.py", line 507, in leave_module
    std_imports, ext_imports, loc_imports = self._check_imports_order(node)
  File "/tmp/pylint_venv/lib/python3.7/site-packages/pylint/checkers/imports.py", line 667, in _check_imports_order
    known_standard_library=self.config.known_standard_library,
  File "/tmp/pylint_venv/lib/python3.7/site-packages/isort/isort.py", line 57, in __init__
    self.config = settings.from_path(settings_path).copy()
  File "/tmp/pylint_venv/lib/python3.7/site-packages/isort/settings.py", line 177, in from_path
    _update_settings_with_config(path, 'pyproject.toml', [], ('tool.isort', ), computed_settings)
  File "/tmp/pylint_venv/lib/python3.7/site-packages/isort/settings.py", line 207, in _update_settings_with_config
    _update_with_config_file(editor_config_file, sections, computed_settings)
  File "/tmp/pylint_venv/lib/python3.7/site-packages/isort/settings.py", line 212, in _update_with_config_file
    settings = _get_config_data(file_path, sections).copy()
  File "/tmp/pylint_venv/lib/python3.7/site-packages/isort/settings.py", line 288, in _get_config_data
    config = toml.load(config_file)
  File "/tmp/pylint_venv/lib/python3.7/site-packages/toml/decoder.py", line 110, in load
    if _ispath(f):
  File "/tmp/pylint_venv/lib/python3.7/site-packages/toml/decoder.py", line 29, in _ispath
    return _detect_pathlib_path(p)
  File "/tmp/pylint_venv/lib/python3.7/site-packages/toml/decoder.py", line 20, in _detect_pathlib_path
    import pathlib
  File "./pathlib.py", line 18, in <module>
    from pathlib import Path, PurePath
ImportError: cannot import name 'Path' from 'pathlib' (./pathlib.py)

Current behavior

Pylint raises an exception if the current worrking directory contains a module names pathlib, that is different from the stdlib version of pathlib. This issue is probably not specific to the pathlib module, but applies to all modules from the stdlib that are shadowed by modules in the cwd that have the same name.

Expected behavior

sys.path must not be modified to contain the CWD.

pylint --version output

(pylint_venv) > $ pylint --version                                                                                                         
pylint 2.3.1
astroid 2.2.5
Python 3.7.3 (default, Apr  3 2019, 05:39:12) 
[GCC 8.3.0]
PCManticore commented 5 years ago

Thanks for the report, we should definitely fix this.