New submission from Daniël van Noord <breeuwer.dragonder...@icloud.com>:

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 <rep...@bugs.python.org>
<https://bugs.python.org/issue46241>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to