Skip to content

Commit 03a4495

Browse files
committed
Add static typing annotations to opencolorio_config_aces.config sub-package.
Signed-off-by: Thomas Mansencal <[email protected]>
1 parent e958dcd commit 03a4495

File tree

12 files changed

+515
-407
lines changed

12 files changed

+515
-407
lines changed

opencolorio_config_aces/config/cg/generate/config.py

Lines changed: 85 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import logging
1515
import re
1616
from collections import defaultdict
17+
from collections.abc import Callable, Mapping
1718
from pathlib import Path
19+
from typing import Any
1820

1921
import PyOpenColorIO as ocio
2022

@@ -23,6 +25,7 @@
2325
discover_clf_transforms,
2426
unclassify_clf_transforms,
2527
)
28+
from opencolorio_config_aces.clf.discover.classify import CLFTransform
2629
from opencolorio_config_aces.config.generation import (
2730
BUILD_CONFIGURATIONS,
2831
BUILD_VARIANT_FILTERERS,
@@ -82,9 +85,9 @@
8285
"main",
8386
]
8487

85-
LOGGER = logging.getLogger(__name__)
88+
LOGGER: logging.Logger = logging.getLogger(__name__)
8689

87-
URL_EXPORT_TRANSFORMS_MAPPING_FILE_CG = (
90+
URL_EXPORT_TRANSFORMS_MAPPING_FILE_CG: str = (
8891
"https://docs.google.com/spreadsheets/d/"
8992
"1PXjTzBVYonVFIceGkLDaqcEJvKR6OI63DwZX0aajl3A/"
9093
"export?format=csv&gid=365242296"
@@ -95,7 +98,7 @@
9598
URL_EXPORT_TRANSFORMS_MAPPING_FILE_CG : unicode
9699
"""
97100

98-
PATH_TRANSFORMS_MAPPING_FILE_CG = next(
101+
PATH_TRANSFORMS_MAPPING_FILE_CG: Path = next(
99102
(Path(__file__).parents[0] / "resources").glob("*Mapping.csv")
100103
)
101104
"""
@@ -104,22 +107,22 @@
104107
PATH_TRANSFORMS_MAPPING_FILE_CG : unicode
105108
"""
106109

107-
FILTERED_NAMESPACES = ("OCIO",)
110+
FILTERED_NAMESPACES: tuple[str, ...] = ("OCIO",)
108111
"""
109112
Filtered namespaces.
110113
111114
FILTERED_NAMESPACES : tuple
112115
"""
113116

114-
TEMPLATE_CLF_TRANSFORM_ID = "CLFtransformID: {}"
117+
TEMPLATE_CLF_TRANSFORM_ID: str = "CLFtransformID: {}"
115118
"""
116119
Template for the description of an *CLFtransformID*.
117120
118121
TEMPLATE_CLF_TRANSFORM_ID : unicode
119122
"""
120123

121124

122-
def is_reference(name):
125+
def is_reference(name: str) -> bool:
123126
"""
124127
Return whether given name represent a reference linear-like space.
125128
@@ -141,7 +144,7 @@ def is_reference(name):
141144
)
142145

143146

144-
def clf_transform_to_colorspace_name(clf_transform):
147+
def clf_transform_to_colorspace_name(clf_transform: CLFTransform) -> str:
145148
"""
146149
Generate the *OpenColorIO* `Colorspace` name for given *CLF* transform.
147150
@@ -165,11 +168,11 @@ def clf_transform_to_colorspace_name(clf_transform):
165168

166169

167170
def clf_transform_to_description(
168-
clf_transform,
169-
describe=DescriptionStyle.LONG_UNION,
170-
amf_components=None,
171-
direction="Forward",
172-
):
171+
clf_transform: CLFTransform,
172+
describe: DescriptionStyle = DescriptionStyle.LONG_UNION,
173+
amf_components: Mapping[str, Any] | None = None,
174+
direction: str = "Forward",
175+
) -> str | None:
173176
"""
174177
Generate the *OpenColorIO* `Colorspace` or `NamedTransform` description for
175178
given *CLF* transform.
@@ -260,7 +263,10 @@ def clf_transform_to_description(
260263
return description
261264

262265

263-
def clf_transform_to_family(clf_transform, filtered_namespaces=FILTERED_NAMESPACES):
266+
def clf_transform_to_family(
267+
clf_transform: CLFTransform,
268+
filtered_namespaces: tuple[str, ...] = FILTERED_NAMESPACES,
269+
) -> str:
264270
"""
265271
Generate the *OpenColorIO* `Colorspace` or `NamedTransform` family for
266272
given *CLF* transform.
@@ -292,12 +298,12 @@ def clf_transform_to_family(clf_transform, filtered_namespaces=FILTERED_NAMESPAC
292298

293299

294300
def clf_transform_to_colorspace(
295-
clf_transform,
296-
describe=DescriptionStyle.LONG_UNION,
297-
amf_components=None,
298-
signature_only=False,
299-
**kwargs,
300-
):
301+
clf_transform: CLFTransform,
302+
describe: DescriptionStyle = DescriptionStyle.LONG_UNION,
303+
amf_components: Mapping[str, Any] | None = None,
304+
signature_only: bool = False,
305+
**kwargs: Any,
306+
) -> dict[str, Any] | ocio.ColorSpace:
301307
"""
302308
Generate the *OpenColorIO* `Colorspace` for given *CLF* transform.
303309
@@ -359,12 +365,12 @@ def clf_transform_to_colorspace(
359365

360366

361367
def clf_transform_to_named_transform(
362-
clf_transform,
363-
describe=DescriptionStyle.LONG_UNION,
364-
amf_components=None,
365-
signature_only=False,
366-
**kwargs,
367-
):
368+
clf_transform: CLFTransform,
369+
describe: DescriptionStyle = DescriptionStyle.LONG_UNION,
370+
amf_components: Mapping[str, Any] | None = None,
371+
signature_only: bool = False,
372+
**kwargs: Any,
373+
) -> dict[str, Any] | ocio.NamedTransform:
368374
"""
369375
Generate the *OpenColorIO* `NamedTransform` for given *CLF* transform.
370376
@@ -404,20 +410,20 @@ def clf_transform_to_named_transform(
404410
"src": clf_transform.path,
405411
}
406412
if is_reference(clf_transform.source):
407-
signature["inverse_transform"] = file_transform
408-
signature["description"] = clf_transform_to_description(
413+
signature["inverse_transform"] = file_transform # pyright: ignore
414+
signature["description"] = clf_transform_to_description( # pyright: ignore
409415
clf_transform, describe, amf_components, direction="Reverse"
410416
)
411417
else:
412-
signature["forward_transform"] = file_transform
413-
signature["description"] = clf_transform_to_description(
418+
signature["forward_transform"] = file_transform # pyright: ignore
419+
signature["description"] = clf_transform_to_description( # pyright: ignore
414420
clf_transform, describe, amf_components, direction="Forward"
415421
)
416422

417423
signature.update(kwargs)
418424

419-
signature["aliases"] = list(
420-
dict.fromkeys([beautify_alias(signature["name"])] + signature["aliases"])
425+
signature["aliases"] = list( # pyright: ignore
426+
dict.fromkeys([beautify_alias(signature["name"])] + signature["aliases"]) # pyright: ignore
421427
)
422428

423429
if signature_only:
@@ -429,13 +435,13 @@ def clf_transform_to_named_transform(
429435

430436

431437
def style_to_colorspace(
432-
style,
433-
describe=DescriptionStyle.LONG_UNION,
434-
amf_components=None,
435-
signature_only=False,
436-
scheme="Modern 1", # noqa: ARG001
437-
**kwargs,
438-
):
438+
style: str,
439+
describe: DescriptionStyle = DescriptionStyle.LONG_UNION,
440+
amf_components: Mapping[str, Any] | None = None,
441+
signature_only: bool = False,
442+
scheme: str = "Modern 1", # noqa: ARG001
443+
**kwargs: Any,
444+
) -> dict[str, Any] | ocio.ColorSpace:
439445
"""
440446
Create an *OpenColorIO* `Colorspace` or its signature for given style.
441447
@@ -539,13 +545,13 @@ def style_to_colorspace(
539545

540546

541547
def style_to_named_transform(
542-
style,
543-
describe=DescriptionStyle.LONG_UNION,
544-
amf_components=None,
545-
signature_only=False,
546-
scheme="Modern 1", # noqa: ARG001
547-
**kwargs,
548-
):
548+
style: str,
549+
describe: DescriptionStyle = DescriptionStyle.LONG_UNION,
550+
amf_components: Mapping[str, Any] | None = None,
551+
signature_only: bool = False,
552+
scheme: str = "Modern 1", # noqa: ARG001
553+
**kwargs: Any,
554+
) -> dict[str, Any] | ocio.NamedTransform:
549555
"""
550556
Create an *OpenColorIO* `NamedTransform` or its signature for given style.
551557
@@ -654,7 +660,7 @@ def style_to_named_transform(
654660
return colorspace
655661

656662

657-
def config_basename_cg(build_configuration):
663+
def config_basename_cg(build_configuration: BuildConfiguration) -> str:
658664
"""
659665
Generate the ACES* Computer Graphics (CG) *OpenColorIO* config
660666
basename, i.e., the filename devoid of directory affix.
@@ -682,7 +688,7 @@ def config_basename_cg(build_configuration):
682688
)
683689

684690

685-
def config_name_cg(build_configuration):
691+
def config_name_cg(build_configuration: BuildConfiguration) -> str:
686692
"""
687693
Generate the ACES* Computer Graphics (CG) *OpenColorIO* config name.
688694
@@ -716,9 +722,9 @@ def config_name_cg(build_configuration):
716722

717723

718724
def config_description_cg(
719-
build_configuration,
720-
describe=DescriptionStyle.SHORT_UNION,
721-
):
725+
build_configuration: BuildConfiguration,
726+
describe: DescriptionStyle = DescriptionStyle.SHORT_UNION,
727+
) -> str:
722728
"""
723729
Generate the ACES* Computer Graphics (CG) *OpenColorIO* config
724730
description.
@@ -756,16 +762,17 @@ def config_description_cg(
756762

757763

758764
def generate_config_cg(
759-
data=None,
760-
config_name=None,
761-
build_configuration=BuildConfiguration(),
762-
validate=True,
763-
describe=DescriptionStyle.SHORT_UNION,
764-
config_mapping_file_path=PATH_TRANSFORMS_MAPPING_FILE_CG,
765-
scheme="Modern 1",
766-
additional_filterers=None,
767-
additional_data=False,
768-
):
765+
data: Any = None,
766+
config_name: str | Path | None = None,
767+
build_configuration: BuildConfiguration = BuildConfiguration(),
768+
validate: bool = True,
769+
describe: DescriptionStyle = DescriptionStyle.SHORT_UNION,
770+
config_mapping_file_path: Path = PATH_TRANSFORMS_MAPPING_FILE_CG,
771+
scheme: str = "Modern 1",
772+
additional_filterers: dict[str, dict[str, list[Callable[[Any], bool]]]]
773+
| None = None,
774+
additional_data: bool = False,
775+
) -> ocio.Config | tuple[ocio.Config, Any, Any, list[CLFTransform], Any]:
769776
"""
770777
Generate the *ACES* Computer Graphics (CG) *OpenColorIO* config.
771778
@@ -861,7 +868,7 @@ def generate_config_cg(
861868
additional_data=True,
862869
)
863870

864-
def clf_transform_from_id(clf_transform_id):
871+
def clf_transform_from_id(clf_transform_id: str) -> CLFTransform | None:
865872
"""
866873
Filter the "CLFTransform" instances matching given "CLFtransformID".
867874
"""
@@ -882,7 +889,7 @@ def clf_transform_from_id(clf_transform_id):
882889

883890
return clf_transform
884891

885-
def clf_transform_from_style(style):
892+
def clf_transform_from_style(style: str) -> CLFTransform | None:
886893
"""Filter the "CLFTransform" instances matching given style."""
887894

888895
filtered_clf_transforms = [
@@ -970,7 +977,7 @@ def clf_transform_from_style(style):
970977

971978
config_mapping[transform_data["colorspace"]].append(transform_data)
972979

973-
def yield_from_config_mapping():
980+
def yield_from_config_mapping() -> Any:
974981
"""Yield the transform data stored in the *CSV* mapping file."""
975982
for transforms_data in config_mapping.values():
976983
yield from transforms_data
@@ -991,12 +998,12 @@ def yield_from_config_mapping():
991998

992999
LOGGER.info("Implicit transforms: %s.", implicit_transforms)
9931000

994-
def implicit_transform_filterer(transform):
1001+
def implicit_transform_filterer(transform: dict[str, Any]) -> bool:
9951002
"""Return whether given transform is an implicit transform."""
9961003

9971004
return transform.get("name") in implicit_transforms
9981005

999-
def transform_filterer(transform):
1006+
def transform_filterer(transform: dict[str, Any]) -> bool:
10001007
"""Return whether given transform must be included."""
10011008

10021009
for transform_data in yield_from_config_mapping():
@@ -1010,14 +1017,18 @@ def transform_filterer(transform):
10101017

10111018
return False
10121019

1013-
def filter_any(array, filterers):
1020+
def filter_any(
1021+
array: list[dict[str, Any]], filterers: list[Callable[[dict[str, Any]], bool]]
1022+
) -> list[dict[str, Any]]:
10141023
"""Filter array elements passing any of the filterers."""
10151024

10161025
filtered = [a for a in array if any(filterer(a) for filterer in filterers)]
10171026

10181027
return filtered
10191028

1020-
def filter_all(array, filterers):
1029+
def filter_all(
1030+
array: list[dict[str, Any]], filterers: list[Callable[[dict[str, Any]], bool]]
1031+
) -> list[dict[str, Any]]:
10211032
"""Filter array elements passing all of the filterers."""
10221033

10231034
filtered = [a for a in array if all(filterer(a) for filterer in filterers)]
@@ -1080,7 +1091,7 @@ def filter_all(array, filterers):
10801091
a["name"] for a in data.colorspaces if a.get("family") == "Display"
10811092
]
10821093

1083-
def implicit_view_filterer(transform):
1094+
def implicit_view_filterer(transform: dict[str, Any]) -> bool:
10841095
"""Return whether given transform is an implicit view."""
10851096

10861097
return all(
@@ -1090,7 +1101,7 @@ def implicit_view_filterer(transform):
10901101
]
10911102
)
10921103

1093-
def view_filterer(transform):
1104+
def view_filterer(transform: dict[str, Any]) -> bool:
10941105
"""Return whether given view transform must be included."""
10951106

10961107
if transform["display"] not in display_names:
@@ -1142,7 +1153,7 @@ def view_filterer(transform):
11421153

11431154
# CLF Transforms & BuiltinTransform Creation
11441155
# ==========================================
1145-
def remove_existing_colorspace(name):
1156+
def remove_existing_colorspace(name: str) -> None:
11461157
"""Remove given existing *ColorSpace* from the current config data."""
11471158

11481159
for i, colorspace in enumerate(data.colorspaces[:]):
@@ -1155,7 +1166,7 @@ def remove_existing_colorspace(name):
11551166

11561167
data.colorspaces.pop(i)
11571168

1158-
def remove_existing_named_transform(name):
1169+
def remove_existing_named_transform(name: str) -> None:
11591170
"""Remove given existing *NamedTransform* from the current config data."""
11601171

11611172
for i, named_transform in enumerate(data.named_transforms[:]):
@@ -1233,7 +1244,7 @@ def remove_existing_named_transform(name):
12331244
clf_transform = clf_transform_from_id(clf_transform_id)
12341245

12351246
attest(
1236-
clf_transform,
1247+
clf_transform is not None,
12371248
f'"{clf_transform_id}" "CLF" transform does not exist!',
12381249
)
12391250

@@ -1313,7 +1324,7 @@ def remove_existing_named_transform(name):
13131324

13141325
# Ordering
13151326
# ========
1316-
def ordering(element):
1327+
def ordering(element: dict[str, Any]) -> int:
13171328
"""Return the ordering key for given element."""
13181329

13191330
return int(
@@ -1345,7 +1356,7 @@ def ordering(element):
13451356
return config
13461357

13471358

1348-
def main(build_directory):
1359+
def main(build_directory: Path) -> int:
13491360
"""
13501361
Define the main entry point for the generation of all the *ACES* Computer
13511362
Graphics (CG) *OpenColorIO* config versions and variants.

0 commit comments

Comments
 (0)