Skip to content

[P3][bug] Constructing OilPriceAPI emits ImportWarning when matplotlib is not installed #151

Description

@karlwaldman

Status

Confirmed on 2026-09-13 against origin/main 0266d08. Found while testing #149: a test that turns all warnings into errors failed when it built a sync client.

Defect

OilPriceAPI.__init__ (oilpriceapi/client.py:224-230) always constructs PriceVisualizer(self). PriceVisualizer.__init__ (oilpriceapi/visualization.py:92-97) calls warnings.warn("matplotlib is required for visualization. Install with: pip install matplotlib", ImportWarning) whenever matplotlib is not installed.

So every sync client built without the optional matplotlib extra warns about a feature the caller has not used.

  • Python's default filters ignore ImportWarning, so plain scripts do not show it.
  • Anything that surfaces warnings does show it: pytest's warning summary, -W error, and warnings.simplefilter("error"/"always").
  • Under -W error or simplefilter("error"), client construction raises.

The except ImportError around the construction does not catch it, because a warning is not an ImportError.

Repro

import warnings
from oilpriceapi import OilPriceAPI

with warnings.catch_warnings():
    warnings.simplefilter("error")
    OilPriceAPI(api_key="-".join(["fixture", "not", "a", "real", "key"]))
# ImportWarning: matplotlib is required for visualization. Install with: pip install matplotlib

Run in an environment with the SDK installed and matplotlib absent.

Expected

Construction emits nothing. Warn, or raise an error that names the extra, only when a client.viz plotting method is actually called without matplotlib.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions