Skip to content

Use of importlib.metadata breaks import when metadata is stripped #15

@JP-Dhabolt

Description

@JP-Dhabolt

Expected Behavior: Importing the module works without bundling Package Metadata in application
Actual Behavior: Stripping Package Metadata breaks imports

Cause:
The pattern of directly using importlib.metadata to set the version assumes that package metadata is included in the location where the package is being used. As a result, this breaks when using slimmed down deployment artifacts, as package metadata is not bundled. I have seen a standard approach to this to be to wrap this self-referential version within a try/except block, e.g.

try:
    __version__ = version("annotated-doc")
except Exception:
    __version__ = "unknown"

https://github.com/fastapi/annotated-doc/blob/7dd21fb75ea6882caf79725881690551ab28c5ff/src/annotated_doc/__init__.py#L5C1-L5C39

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions