Skip to content
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

Add type hints for ImageCms #7913

Merged
merged 16 commits into from Mar 31, 2024
Merged

Add type hints for ImageCms #7913

merged 16 commits into from Mar 31, 2024

Conversation

nulano
Copy link
Contributor

@nulano nulano commented Mar 28, 2024

Changes proposed in this pull request:

  • Add type hints for ImageCms and ImageCms.core;
  • Add type check to ImageCms.core.profile_tobytes - this cannot be called via the non-core functions thanks to a type check in ImageCms.ImageCmsProfile().
  • Update types in ImageCms.rst to match the new ones in _imagingcms.pyi.

nitpick_ignore = [
# sphinx does not understand `typing.Literal[-1]`
("py:obj", "typing.Literal[-1, 1]"),
]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be fixed in a future release of sphinx: sphinx-doc/sphinx#11904

/* Ready object types */
PyType_Ready(&CmsProfile_Type);
PyType_Ready(&CmsTransform_Type);

Py_INCREF(&CmsProfile_Type);
PyModule_AddObject(m, "CmsProfile", (PyObject *)&CmsProfile_Type);

Py_INCREF(&CmsTransform_Type);
PyModule_AddObject(m, "CmsTransform", (PyObject *)&CmsTransform_Type);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seemed odd to me to expose one of the classes but not the other.

@@ -1511,15 +1511,16 @@ setup_module(PyObject *m) {
PyObject *v;
int vn;

CmsProfile_Type.tp_new = PyType_GenericNew;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed (and incorrect! should call core.profile_open or core.profile_frombytes instead).

I added new tests for this being unset, but on PyPy this is the default value.

@@ -787,14 +797,17 @@ def createProfile(colorSpace, colorTemp=-1):
except (TypeError, ValueError) as e:
msg = f'Color temperature must be numeric, "{colorTemp}" not valid'
raise PyCMSError(msg) from e
else:
# colorTemp is unused if colorSpace != "LAB"
colorTemp = 0.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is minor, but considering it's unused, what's the advantage of setting it to zero?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value is passed to PyArg_ParseTuple with the d format. Based on the documentation, my understanding was that this required a float object, not a SupportsFloat object. However, testing with Python 3.11, it seems that a SupportsFloat object is actually accepted, so I'll revert this part of the change.

(Testing with SupportsInt I do get the expected TypeError, so at least that part matches the documentation.)

outMode: str,
renderingIntent: Intent = Intent.PERCEPTUAL,
proofRenderingIntent: Intent = Intent.ABSOLUTE_COLORIMETRIC,
flags: Flags | int = Flags.SOFTPROOFING,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't our discussion in #7671 (comment) about favouring enums over int also apply to the IntFlag of Flags, meaning this could just be flags: Flags?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, you're right. IIRC I might have added int to support the deprecated ImageCms.FLAGS, but that does not have type hints and should not be used anyway.

docs/conf.py Outdated Show resolved Hide resolved
nulano and others added 2 commits March 30, 2024 10:26
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
@hugovk hugovk merged commit 6c55ab2 into python-pillow:main Mar 31, 2024
59 checks passed
@hugovk
Copy link
Member

hugovk commented Mar 31, 2024

Thanks!

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

Successfully merging this pull request may close these issues.

None yet

3 participants