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

pylint files gets imported when a file has the same name as one of pylint's files #2258

Closed
adamtheturtle opened this issue Jul 5, 2018 · 7 comments

Comments

@adamtheturtle
Copy link
Contributor

An erroneous error occurs when a file is named "config.py".

Steps to reproduce

  1. Create a structure as so:
> tree pylint-experiment/
pylint-experiment/
└── example
    ├── config.py
    └── foo.py
> cat pylint-experiment/example/foo.py
import config

print(config.VARIABLE)
> cat pylint-experiment/example/config.py
VARIABLE = 1
  1. pylint --errors-only pylint-experiment/

Current behavior

************* Module some_dir.other
pylint-experiment/some_dir/other.py:3:6: E1101: Module 'config' has no 'VARIABLE' member (no-member)

Expected behavior

I expect to see:

************* Module example.foo
pylint-experiment/example/foo.py:1:0: E0401: Unable to import 'config' (import-error)

This is what happens if I rename config to bar and change import config to import bar.

pylint --version output

pylint 2.0.0.dev2
astroid 2.0.0.dev4
Python 3.7.0 (default, Jun 29 2018, 20:13:13) 
[Clang 9.1.0 (clang-902.0.39.2)]
@brycepg
Copy link
Contributor

brycepg commented Jul 5, 2018

This is working as intended since you are specifying a namespace/package to lint (since a folder is automatically a namespace package in python3). Python3 does not support relative imports for packages, so you see the error above.

You can see this in action with the python repl:

cd pylint-experiment
python
> from example import foo
ModuleNotFoundError: No module named 'config'

If you want to lint your script, foo.py, run pylint pylint-experiment/example/foo.py

@adamtheturtle
Copy link
Contributor Author

@brycepg I expected the import-error, as per "Expected behavior", but instead there is a no-member error, which happens when the file is named config.py.

@adamtheturtle
Copy link
Contributor Author

That is, I'd expect the same error from both of these:

> pylint -E pylint-experiment-changed-names/
************* Module example.foo
pylint-experiment-changed-names/example/foo.py:1:0: E0401: Unable to import 'bar' (import-error)
> pylint -E pylint-experiment
************* Module example.foo
pylint-experiment/example/foo.py:3:6: E1101: Module 'config' has no 'VARIABLE' member (no-member)

Where pylint-experiment-changed-names/ is the same as the given example, but with config.py renamed to bar.py and the import in foo.py changed accordingly.

@brycepg
Copy link
Contributor

brycepg commented Jul 5, 2018

Oh my bad, you're right. This also applies to explicit packages

@brycepg brycepg changed the title Error with file named "config.py" Relative imports in packages shadow import-error Jul 5, 2018
@PCManticore
Copy link
Contributor

So the super silly bug is that if your file is named config.py, then pylint imports its own config.py file:

In [3]: owner.file # where owner is the module that raised the error
Out[3]: '/Users/claudiu/projects/personal/pylint/pylint/config.py'

In [4]: owner.path
Out[4]: ['/Users/claudiu/projects/personal/pylint/pylint/config.py']

I bet the same happens for all the files in the pylint directory.

@PCManticore PCManticore changed the title Relative imports in packages shadow import-error pylint files gets imported when a file has the same name as one of pylint's files Jul 17, 2018
@PCManticore PCManticore added this to the Next bug fix release milestone Jul 17, 2018
@nickroeker
Copy link

nickroeker commented Jul 25, 2018

I'm experiencing what I think is a similar issue, tested with pylint version 1.9.3 and 2.0.1.

Repro Setup

Given the following setup,

config package:

$ tree config/
config/
└── subpackage
    └── __init__.py

message package:

$ tree message/
message/
└── subpackage
    └── __init__.py

very_custom_package package:

$ tree very_custom_package/
very_custom_package/
└── subpackage
    └── __init__.py

A test file for importing those packages:

$ tree tests/
tests/
├── __init__.py
└── test_packages.py

Contents of test file:

$ cat tests/test_packages.py 
"""Notice we get errors for `message` & `config`, but not `very_custom_package`."""
import config.subpackage
import message.subpackage
import very_custom_package.subpackage

print(config.subpackage.desc)
print(message.subpackage.desc)
print(very_custom_package.subpackage.desc)

Problem

If I run pylint tests/ (checked with both 1.9.3 and 2.0.1), I get erroneous complaints about the config and message packages, but not very_custom_package.

pyilnt 1.9.3 output:

$ pylint tests/
No config file found, using default configuration
************* Module tests.test_packages
E:  2, 0: No name 'subpackage' in module 'config' (no-name-in-module)
E:  3, 0: No name 'subpackage' in module 'message' (no-name-in-module)
E:  6, 6: Module 'config' has no 'subpackage' member (no-member)
E:  7, 6: Module 'message' has no 'subpackage' member (no-member)

pylint 2.0.1 output:

$ pylint tests/
************* Module tests.test_packages
tests/test_packages.py:2:0: E0611: No name 'subpackage' in module 'config' (no-name-in-module)
tests/test_packages.py:3:0: E0611: No name 'subpackage' in module 'message' (no-name-in-module)
tests/test_packages.py:6:6: E1101: Module 'config' has no 'subpackage' member (no-member)
tests/test_packages.py:7:6: E1101: Module 'message' has no 'subpackage' member (no-member)

Expected output

No errors for the message and config packages, like the very_custom_package package.

Things I've discovered

  • Applies only to namespace packages as far as I can tell (perhaps even just implicit namespace packages). Adding an __init__.py resolves the false-positive, but isn't desirable.
  • Modifying the names slightly (e.g. config -> zconfig) results in the errors going away, so somehow the specific names matter & it's not related to the package being named with underscores.
  • With other issues present as well, I've tested as far back as 1.7.0 and get the same sort of issue.

@mbyrnepr2
Copy link
Member

I can't reproduce this (the example in the description or this example from the comments).
I haven't checked if it is covered by tests but looks like we can close if it is.

pylint 2.16.0-dev
astroid 2.12.12
Python 3.10.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants