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

ValueError: PurePosixPath('.') has an empty name #10764

Closed
chingc opened this issue Feb 24, 2023 · 8 comments
Closed

ValueError: PurePosixPath('.') has an empty name #10764

chingc opened this issue Feb 24, 2023 · 8 comments
Labels
stale status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously

Comments

@chingc
Copy link

chingc commented Feb 24, 2023

Hi, I'm running Python 3.11.2 and pytest 7.2.1 on macOS (Ventura 13.1) with Apple Silicon and I'm running into the following error:

ValueError: PurePosixPath('.') has an empty name

I was able to trace the error to this change: f92c4a7

The error occurs when calling with_suffix when parts is an empty list. Here's an example:

Python 3.11.2 (main, Feb 16 2023, 02:55:59) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import os
>>> from pathlib import PurePath
>>> parts = ['']

# result is the same when not dealing with the suffix
>>> PurePath(os.path.sep.join(parts)) == PurePath(*parts)
True

# old code: empty parts with suffix returns the suffix
>>> PurePath(os.path.sep.join(parts) + ".py")
PurePosixPath('.py')

# new code: empty parts with suffix throws an exception
>>> PurePath(*parts).with_suffix(".py")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/homebrew/Cellar/python@3.11/3.11.2_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 694, in with_suffix
    raise ValueError("%r has an empty name" % (self,))
ValueError: PurePosixPath('.') has an empty name

I haven't been able to figure out why parts is ever empty, but with what I know I was able to temporarily workaround this problem by downgrading to pytest 7.1.2.

@Zac-HD Zac-HD added type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously labels Mar 5, 2023
@theirix
Copy link
Contributor

theirix commented Jun 1, 2023

Could you please describe a project layout or example where you have got this error? Could not reproduce it.

@RonnyPfannschmidt RonnyPfannschmidt added the status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity label Jun 2, 2023
@github-actions
Copy link
Contributor

This issue is stale because it has been open for 14 days with no activity.

@github-actions github-actions bot added the stale label Jun 17, 2023
@github-actions
Copy link
Contributor

This issue was closed because it has been inactive for 7 days since being marked as stale.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 24, 2023
@jgarvin
Copy link

jgarvin commented Sep 14, 2023

@theirix can confirm this still exists. I use this code to import a module at runtime:

def _import_from_filename(file_path: Path) -> ModuleType:
    """Try to import with exactly the module path we would have gotten
    had we imported manually."""
    relative_path = file_path.strict_relative_to(SOURCE_ROOT.get())
    module_name = str(relative_path.with_suffix('')).replace('/', '.')

    if module_name in sys.modules:
        log.info(f"Already imported {module_name}")
        return sys.modules[module_name]  # already imported

    log.info(f"Importing module={file_path}")
    spec = importlib.util.spec_from_file_location(module_name, file_path)
    assert spec
    assert spec.loader
    module = importlib.util.module_from_spec(spec)
    sys.modules[module_name] = module
    spec.loader.exec_module(module)
    return module

that module contains a relative import:

from .rule_a import a_rule

which triggers the error. the import has to be relative in order to trigger

@RonnyPfannschmidt
Copy link
Member

#11426 should be there fix

@jgarvin
Copy link

jgarvin commented Sep 14, 2023

@RonnyPfannschmidt I did a fresh python3.11 -m pip install 'pytest @ git+https://github.com/pytest-dev/pytest.git' but still get the error

@The-Compiler
Copy link
Member

Can you please share the full output and ideally a reproducer we can run?

@nicoddemus
Copy link
Member

Wait, what does the code posted in #10764 (comment) has to do with pytest? IIUC, that's custom code which uses importlib, but has no relation to pytest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously
Projects
None yet
Development

No branches or pull requests

7 participants