From 231c94591cca11976ca4b3667e2149f18a37daea Mon Sep 17 00:00:00 2001 From: Thomas Mansencal Date: Tue, 7 Nov 2023 19:31:34 +1300 Subject: [PATCH 1/2] Implement support for injecting `vaab/colour` objects into our namespace. --- colour/__init__.py | 71 +++++++++++++++++++++++++++++++++++++++++++++- docs/advanced.rst | 4 +++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/colour/__init__.py b/colour/__init__.py index 67765b180..a9c7cdad6 100644 --- a/colour/__init__.py +++ b/colour/__init__.py @@ -45,6 +45,7 @@ """ import contextlib +import os import sys import numpy as np @@ -948,7 +949,7 @@ def __getattr__(self, attribute) -> Any: sys.modules["colour"], build_API_changes(API_CHANGES) ) - del ModuleAPI, is_documentation_building, build_API_changes, sys + del ModuleAPI, is_documentation_building, build_API_changes colour.__disable_lazy_load__ = True # pyright: ignore __disable_lazy_load__ = colour.__disable_lazy_load__ # pyright: ignore @@ -956,3 +957,71 @@ def __getattr__(self, attribute) -> Any: Ensures that the lazy loaded datasets are not transformed during import. See :class:`colour.utilities.LazyCanonicalMapping` for more information. """ + +# NOTE: We are solving the clash with https://github.com/vaab/colour by loading +# a known subset of the objects given by vaab/colour-0.1.5 into our namespace. +# There haven't been many clashes over the years but people using +# https://www.manim.community are experiencing problems: +# - https://github.com/colour-science/colour/issues/958 +# - https://github.com/colour-science/colour/issues/1221 +if not os.environ.get( + "COLOUR_SCIENCE__COLOUR__SKIP_VAAB_COLOUR_INJECTION" +): # pragma: no cover + for _path in sys.path: + _module_path = os.path.join(_path, "colour.py") + if os.path.exists(_module_path): + import importlib.machinery + + import colour # pyright: ignore + + _module = importlib.machinery.SourceFileLoader( + "__vaab_colour__", _module_path + ).load_module() + + for name in [ + "COLOR_NAME_TO_RGB", + "C_HEX", + "C_HSL", + "C_RGB", + "Color", + "HEX", + "HSL", + "HSL_equivalence", + "LONG_HEX_COLOR", + "RGB", + "RGB_TO_COLOR_NAMES", + "RGB_color_picker", + "RGB_equivalence", + "SHORT_HEX_COLOR", + "color_scale", + "hash_or_str", + "hex2hsl", + "hex2rgb", + "hex2web", + "hsl2hex", + "hsl2rgb", + "hsl2web", + "make_color_factory", + "rgb2hex", + "rgb2hsl", + "rgb2web", + "web2hex", + "web2hsl", + "web2rgb", + ]: + if name in dir(_module): + colour.utilities.warning( # pyright: ignore + f'Injecting "vaab/colour" "{name}" object into ' + f'"Colour"\'s namespace!' + ) + setattr( + sys.modules["colour"], name, getattr(_module, name) + ) + + del importlib, _module + + break + + del _module_path, _path + +del os, sys diff --git a/docs/advanced.rst b/docs/advanced.rst index 801467d41..6c3a320b2 100644 --- a/docs/advanced.rst +++ b/docs/advanced.rst @@ -22,6 +22,10 @@ runtime: :func:`warnings.showwarning` definition to be replaced with the :func:`colour.utilities.show_warning` definition and thus providing complete traceback from the point where the warning occurred. +- `COLOUR_SCIENCE__COLOUR__SKIP_VAAB_COLOUR_INJECTION`: Skip + `vaab/colour `__ objects injection into + **Colour** namespace. See the bottom of the core `__init__.py` module for + more information. Caching ------- From 6e828f651a702961cada2bff683c68eda3f8a551 Mon Sep 17 00:00:00 2001 From: Thomas Mansencal Date: Wed, 8 Nov 2023 21:54:35 +1300 Subject: [PATCH 2/2] Disable default import of *vaab/colour* objects. --- colour/__init__.py | 118 ++++++++++++++++++++++++--------------------- docs/advanced.rst | 9 ++-- 2 files changed, 67 insertions(+), 60 deletions(-) diff --git a/colour/__init__.py b/colour/__init__.py index a9c7cdad6..c507aaf53 100644 --- a/colour/__init__.py +++ b/colour/__init__.py @@ -959,69 +959,75 @@ def __getattr__(self, attribute) -> Any: """ # NOTE: We are solving the clash with https://github.com/vaab/colour by loading -# a known subset of the objects given by vaab/colour-0.1.5 into our namespace. -# There haven't been many clashes over the years but people using -# https://www.manim.community are experiencing problems: +# a known subset of the objects given by vaab/colour-0.1.5 into our namespace +# if the *COLOUR_SCIENCE__COLOUR__IMPORT_VAAB_COLOUR=True* environment variable +# is defined. +# +# See the following issues for more information: # - https://github.com/colour-science/colour/issues/958 # - https://github.com/colour-science/colour/issues/1221 -if not os.environ.get( - "COLOUR_SCIENCE__COLOUR__SKIP_VAAB_COLOUR_INJECTION" -): # pragma: no cover - for _path in sys.path: - _module_path = os.path.join(_path, "colour.py") - if os.path.exists(_module_path): - import importlib.machinery +# - https://github.com/vaab/colour/issues/62 +for _path in sys.path: + _module_path = os.path.join(_path, "colour.py") + if os.path.exists(_module_path): + import colour # pyright: ignore - import colour # pyright: ignore + if not os.environ.get("COLOUR_SCIENCE__COLOUR__IMPORT_VAAB_COLOUR"): + colour.utilities.warning( # pyright: ignore + '"vaab/colour" was detected in "sys.path", please define a ' + '"COLOUR_SCIENCE__COLOUR__IMPORT_VAAB_COLOUR=True" environment ' + 'variable to import its objects into "colour" namespace!' + ) + break - _module = importlib.machinery.SourceFileLoader( - "__vaab_colour__", _module_path - ).load_module() + import importlib.machinery - for name in [ - "COLOR_NAME_TO_RGB", - "C_HEX", - "C_HSL", - "C_RGB", - "Color", - "HEX", - "HSL", - "HSL_equivalence", - "LONG_HEX_COLOR", - "RGB", - "RGB_TO_COLOR_NAMES", - "RGB_color_picker", - "RGB_equivalence", - "SHORT_HEX_COLOR", - "color_scale", - "hash_or_str", - "hex2hsl", - "hex2rgb", - "hex2web", - "hsl2hex", - "hsl2rgb", - "hsl2web", - "make_color_factory", - "rgb2hex", - "rgb2hsl", - "rgb2web", - "web2hex", - "web2hsl", - "web2rgb", - ]: - if name in dir(_module): - colour.utilities.warning( # pyright: ignore - f'Injecting "vaab/colour" "{name}" object into ' - f'"Colour"\'s namespace!' - ) - setattr( - sys.modules["colour"], name, getattr(_module, name) - ) + _module = importlib.machinery.SourceFileLoader( + "__vaab_colour__", _module_path + ).load_module() - del importlib, _module + for name in [ + "COLOR_NAME_TO_RGB", + "C_HEX", + "C_HSL", + "C_RGB", + "Color", + "HEX", + "HSL", + "HSL_equivalence", + "LONG_HEX_COLOR", + "RGB", + "RGB_TO_COLOR_NAMES", + "RGB_color_picker", + "RGB_equivalence", + "SHORT_HEX_COLOR", + "color_scale", + "hash_or_str", + "hex2hsl", + "hex2rgb", + "hex2web", + "hsl2hex", + "hsl2rgb", + "hsl2web", + "make_color_factory", + "rgb2hex", + "rgb2hsl", + "rgb2web", + "web2hex", + "web2hsl", + "web2rgb", + ]: + if name in dir(_module): + colour.utilities.warning( # pyright: ignore + f'Importing "vaab/colour" "{name}" object into ' + f'"Colour"\'s namespace!' + ) + setattr(sys.modules["colour"], name, getattr(_module, name)) - break + del importlib, _module + + break - del _module_path, _path + del _module_path, _path del os, sys diff --git a/docs/advanced.rst b/docs/advanced.rst index 6c3a320b2..3caac5197 100644 --- a/docs/advanced.rst +++ b/docs/advanced.rst @@ -22,10 +22,11 @@ runtime: :func:`warnings.showwarning` definition to be replaced with the :func:`colour.utilities.show_warning` definition and thus providing complete traceback from the point where the warning occurred. -- `COLOUR_SCIENCE__COLOUR__SKIP_VAAB_COLOUR_INJECTION`: Skip - `vaab/colour `__ objects injection into - **Colour** namespace. See the bottom of the core `__init__.py` module for - more information. +- `COLOUR_SCIENCE__COLOUR__IMPORT_VAAB_COLOUR`: Import + `vaab/colour `__ injection into + **Colour** namespace. This solves the clash with + `vaab/colour `__ by loading a known subset + of the objects given by vaab/colour-0.1.5 into our namespace. Caching -------