-
Notifications
You must be signed in to change notification settings - Fork 11
Literal annotation for named color maps #106
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
Conversation
tlambert03
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is great! 👍
Untitled.mov
left some minor comments below
src/cmap/_colormap.py
Outdated
| # All of the things that we can pass to the constructor of Colormap | ||
| ColormapLike: TypeAlias = Union[ | ||
| str, # colormap name, w/ optional "_r" suffix | ||
| ColormapName, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ColormapName, | |
| ColormapName, | |
| str, |
I know it feels like a cop-out, having added such lovely completion here. But from past experiences, I have a feeling this will result in people using # type: ignore all over the place rather than doing the research to import ColormapName to make their type checker happy. By adding str, we don't lose the lovely autocompletion and discovery.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what would you think about making this file .pyi? We don't ever want the compiler to have to make byte-code for it. It would require/demand import inside of a TYPE_CHECKING clause. (You could also export it in cmap/__init__.py, inside of TYPE_CHECKING, for those who want to use it externally)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(would also fix the failing 3.9 tests)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
scripts/make_cmapnames.py
Outdated
| """Produce the expected script.""" | ||
| names = [] | ||
|
|
||
| for rel_path in sorted(data_dir.glob("*/record.json")): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since ultimately, strings passed to Colormap are compared against the catalog:
Lines 232 to 234 in fb69861
| if isinstance(value, str): | |
| rev = value.endswith("_r") | |
| info = self.catalog()[value[:-2] if rev else value] |
you could also use the catalog directly here, rather than parsing record.json again. This would avoid any divergence in logic for how the catalog is constructed from records.
import cmap
list(Colormap.catalog()) # all the colormap names, without the `_r`not sure if you were trying to refrain from importing cmap? I don't see any issues with that...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm now using the Catalog, although I am using the normalised names for consistency with the docs. I added some logic to avoid ambiguous alias warnings.
scripts/make_cmapnames.py
Outdated
| scripts_dir = Path(__file__).resolve().parent | ||
| module_dir = scripts_dir.parent.joinpath("src", "cmap") | ||
| data_dir = module_dir.joinpath("data") | ||
| out_file = module_dir.joinpath("_colormapname.py") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.pyi... depending on how you felt about that suggestion in my other comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've now set this up as a pyi, which passes mypy although I still get a yellow squiggly in my editor (VSCode).
|
Ah, depending on cmap means you can only run it when you have cmap installed, which isn't ideal e.g. on CI. What do you think? |
|
|
|
see #108 (comment) |
See #105