[issue46241] importlib.utils.find_spec() has issues with "tests" folder

2022-01-05 Thread Daniël van Noord

Daniël van Noord  added the comment:

Thanks Brett for that explanation!

Makes sense actually now that I think about it. I also wondered how astroid got 
onto my sys.path, but that's something for another time :)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46241] importlib.utils.find_spec() has issues with "tests" folder

2022-01-05 Thread Brett Cannon


Brett Cannon  added the comment:

This is working as intended. Because you don't have a `__init__.py` file in 
your `tests` directory, Python considers it a potential namespace package. As 
such, Python keeps searching for a "normal" package that defines `__init__.py` 
for the same package name. Since Astroid has one and seemingly has it on 
`sys.path` in such a way as to get discovered, it gets selected as the package 
to use for the name `test`.

If you add a `tests/__init__.py` file then your issue will go away.

--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46241] importlib.utils.find_spec() has issues with "tests" folder

2022-01-03 Thread Daniël van Noord

Change by Daniël van Noord :


--
components: +Library (Lib)

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46241] importlib.utils.find_spec() has issues with "tests" folder

2022-01-03 Thread Daniël van Noord

New submission from Daniël van Noord :

Affected folder structure:
── module_loader
│   ├── __init__.py
├── pyproject.toml
├── setup.cfg
└── tests
└── test_module_loader.py

```console
❯ cat 
'/Users/daniel/DocumentenLaptop/Programming/Test/module_loader/module_loader/__init__.py'
from importlib import util


def load_a_spec(modname):
return util.find_spec(modname)
```

```console
❯ cat 
'/Users/daniel/DocumentenLaptop/Programming/Test/module_loader/tests/test_module_loader.py'
from module_loader import load_a_spec


def test_me():
load_a_spec("tests")
```

When I run `pytest` from within the `module_loader` directory and inspect what 
`load_a_spec` returns I find that it returns:

ModuleSpec(name='tests', loader=<_frozen_importlib_external.SourceFileLoader 
object at 0x10a4b4820>, 
origin='/Users/daniel/DocumentenLaptop/Programming/Github/astroid/tests/__init__.py',
 
submodule_search_locations=['/Users/daniel/DocumentenLaptop/Programming/Github/astroid/tests'])

It seems to take the `tests` folder from another project, in this case astroid, 
and returns its spec. 
This is my `sys.path`, which might be relevant:

```console
for i in sys.path:print(i)
/Users/daniel/DocumentenLaptop/Programming/Test/module_loader/tests
/usr/local/bin
/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python39.zip
/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9
/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload
/Users/daniel/Library/Python/3.9/lib/python/site-packages
/usr/local/lib/python3.9/site-packages
/Users/daniel/DocumentenLaptop/Programming/Github/astroid
/Users/daniel/DocumentenLaptop/Programming/Github/DanielNoord/docstringformatter
/Users/daniel/DocumentenLaptop/Programming/Test/module_loader
/usr/local/Cellar/pybind11/2.8.1/libexec/lib/python3.9/site-packages
```

Let me know if I'm doing anything wrong, but I would expect the returned spec 
to be from `module_loader` instead of another project.

--
files: module_loader.zip
messages: 409579
nosy: brett.cannon, danielnoord
priority: normal
severity: normal
status: open
title: importlib.utils.find_spec() has issues with "tests" folder
type: behavior
versions: Python 3.10
Added file: https://bugs.python.org/file50538/module_loader.zip

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com