Skip to content

Mypyc should support __file__ #17795

Open
@CoolCat467

Description

@CoolCat467

Feature

Mypyc should set __file__

Pitch

Getting installation directory is important when referencing data files included in your package.
Things like this are common:

DATA_FOLDER: Final = Path(__file__).parent / "data"

Currently, mypyc will compile this with no warnings about possible issues and crash with

Traceback (most recent call last):
  <unrelated details>
  File "src/checkers/game.py", line 108, in <module>
    DATA_FOLDER: Final = Path(__file__).parent / "data"
KeyError: '__file__'

There is a workaround for this, doing something like this before hand

if globals().get("__file__") is None:
    import inspect
    __file__ = inspect.stack()[0].filename

but that's messy

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions