matplotlib / matplotlib/cmocean

Static type checkers can't recognize colormaps

Open
#120 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
269
Forks
54
PR merge metrics
No merged PRs in 30d

Description

Static type checkers like MyPy or Pylance can't recognize variables created dynamically with [`locals().update(cmap_d)`](https://github.com/matplotlib/cmocean/blob/59c35002c3aa5296b65d9646e52604c627441eb6/cmocean/cm.py#L84). Past just type checking, Pylance can't even recognize that colormaps exist, e.g., `cmocean.cm.balance` produces an [attribute access issue error](https://github.com/microsoft/pylance-release/blob/main/docs/diagnostics/reportAttributeAccessIssue.md) in Pylance.

Matplotlib colormaps have the same issue, which they solve by including a stub file [cm.pyi](https://github.com/matplotlib/matplotlib/blob/main/lib/matplotlib/cm.pyi) that defines the attributes and types.

If there's interest in include a cm.pyi file here, it is straightforward to generate one automatically based on the colormaps defined in `cmap_d` and I'd be happy to make a PR with the details.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Inspect cmocean/cm.py and the cmap_d definition to see how colormaps are created dynamically. Compare the referenced Matplotlib cm.pyi stub and determine how a generated stub would expose the colormap attributes and types; done means MyPy and Pylance recognize entries such as cmocean.cm.balance.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data-visualization
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.