From 0146479bdb0143d09ceba16a07f3021cc938c4d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Mon, 6 Dec 2021 22:28:58 +0100 Subject: [PATCH] TYP: fix typing --- yt/data_objects/static_output.py | 4 +-- yt/frontends/ramses/definitions.py | 4 +-- yt/mods.py | 4 +-- yt/utilities/configure.py | 5 ++-- yt/utilities/logger.py | 29 +++++++++---------- .../volume_rendering/render_source.py | 4 +-- 6 files changed, 23 insertions(+), 27 deletions(-) diff --git a/yt/data_objects/static_output.py b/yt/data_objects/static_output.py index bc2ea10b453..7c0b93f8977 100644 --- a/yt/data_objects/static_output.py +++ b/yt/data_objects/static_output.py @@ -43,7 +43,7 @@ unit_system_registry, ) from yt.units.yt_array import YTArray, YTQuantity -from yt.utilities.configure import configuration_callbacks +from yt.utilities.configure import YTConfig, configuration_callbacks from yt.utilities.cosmology import Cosmology from yt.utilities.exceptions import ( YTFieldNotFound, @@ -77,7 +77,7 @@ _ds_store: Optional[ParameterFileStore] = None -def _setup_ds_store(ytcfg): +def _setup_ds_store(ytcfg: YTConfig) -> None: global _ds_store _ds_store = ParameterFileStore() diff --git a/yt/frontends/ramses/definitions.py b/yt/frontends/ramses/definitions.py index 2e68ce01855..3e57cfc44fb 100644 --- a/yt/frontends/ramses/definitions.py +++ b/yt/frontends/ramses/definitions.py @@ -2,7 +2,7 @@ import re from yt.funcs import mylog -from yt.utilities.configure import configuration_callbacks +from yt.utilities.configure import YTConfig, configuration_callbacks def ramses_header(hvals): @@ -70,7 +70,7 @@ def ramses_header(hvals): } -def _setup_ramses_particle_families(ytcfg): +def _setup_ramses_particle_families(ytcfg: YTConfig) -> None: if not ytcfg.has_section("ramses-families"): return for key in particle_families.keys(): diff --git a/yt/mods.py b/yt/mods.py index b73279d748b..2f731a32acf 100644 --- a/yt/mods.py +++ b/yt/mods.py @@ -15,7 +15,7 @@ from yt import * from yt._maintenance.deprecation import issue_deprecation_warning from yt.config import ytcfg, ytcfg_defaults -from yt.utilities.logger import _level +from yt.utilities.logger import ytLogger issue_deprecation_warning( "The yt.mods module is deprecated.", since="4.1.0", removal="4.2.0" @@ -24,7 +24,7 @@ unparsed_args = __startup_tasks.unparsed_args -if _level >= int(ytcfg_defaults["yt"]["log_level"]): # type: ignore +if ytLogger.getEffectiveLevel() >= int(ytcfg_defaults["yt"]["log_level"]): # type: ignore # This won't get displayed. mylog.debug("Turning off NumPy error reporting") np.seterr(all="ignore") diff --git a/yt/utilities/configure.py b/yt/utilities/configure.py index d15e3a330c6..5f226afaaa0 100644 --- a/yt/utilities/configure.py +++ b/yt/utilities/configure.py @@ -1,13 +1,12 @@ import os -from types import FunctionType -from typing import List +from typing import Callable, List import toml from more_itertools import always_iterable from yt.utilities.configuration_tree import ConfigLeaf, ConfigNode -configuration_callbacks: List[FunctionType] = [] +configuration_callbacks: List[Callable[["YTConfig"], None]] = [] def config_dir(): diff --git a/yt/utilities/logger.py b/yt/utilities/logger.py index 0719e4317f0..e6b3b6ec049 100644 --- a/yt/utilities/logger.py +++ b/yt/utilities/logger.py @@ -1,9 +1,11 @@ import logging import sys +from typing import Callable, Optional -from yt.utilities.configure import configuration_callbacks +from yt.utilities.configure import YTConfig, configuration_callbacks -# from yt.config import ytcfg +_yt_sh: Optional[logging.StreamHandler] = None +_original_emitter: Optional[Callable[[logging.LogRecord], None]] = None def set_log_level(level): @@ -111,16 +113,11 @@ def colorize_logging(): def uncolorize_logging(): - global original_emitter - try: + global _original_emitter, _yt_sh + if None not in (_original_emitter, _yt_sh): f = logging.Formatter(ufstring) ytLogger.handlers[0].setFormatter(f) - yt_sh.emit = original_emitter - except NameError: - # yt_sh and original_emitter are not defined because - # suppress_stream_logging is True, so we continue since there is nothing - # to uncolorize - pass + _yt_sh.emit = _original_emitter def disable_stream_logging(): @@ -130,10 +127,10 @@ def disable_stream_logging(): ytLogger.addHandler(h) -def _runtime_configuration(ytcfg): +def _runtime_configuration(ytcfg: YTConfig) -> None: # only run this at the end of yt.__init__, after yt.config.ytcfg was instanciated - global original_emitter + global _original_emitter, _yt_sh if ytcfg.get("yt", "stdout_stream_logging"): stream = sys.stdout @@ -145,16 +142,16 @@ def _runtime_configuration(ytcfg): if ytcfg.get("yt", "suppress_stream_logging"): disable_stream_logging() else: - yt_sh = logging.StreamHandler(stream=stream) + _yt_sh = logging.StreamHandler(stream=stream) # create formatter and add it to the handlers formatter = logging.Formatter(ufstring) - yt_sh.setFormatter(formatter) + _yt_sh.setFormatter(formatter) # add the handler to the logger - ytLogger.addHandler(yt_sh) + ytLogger.addHandler(_yt_sh) ytLogger.setLevel(_level) ytLogger.propagate = False - original_emitter = yt_sh.emit + _original_emitter = _yt_sh.emit if ytcfg.get("yt", "colored_logs"): colorize_logging() diff --git a/yt/visualization/volume_rendering/render_source.py b/yt/visualization/volume_rendering/render_source.py index 9e63941d3d3..e1ea30d7778 100644 --- a/yt/visualization/volume_rendering/render_source.py +++ b/yt/visualization/volume_rendering/render_source.py @@ -10,7 +10,7 @@ from yt.geometry.grid_geometry_handler import GridIndex from yt.geometry.oct_geometry_handler import OctreeIndex from yt.utilities.amr_kdtree.api import AMRKDTree -from yt.utilities.configure import configuration_callbacks +from yt.utilities.configure import YTConfig, configuration_callbacks from yt.utilities.lib.bounding_volume_hierarchy import BVH from yt.utilities.lib.misc_utilities import zlines, zpoints from yt.utilities.lib.octree_raytracing import OctreeRayTracing @@ -42,7 +42,7 @@ mesh_construction: OptionalModule = NotAModule("pyembree") -def _setup_raytracing_engine(ytcfg): +def _setup_raytracing_engine(ytcfg: YTConfig) -> None: global mesh_traversal, mesh_construction try: from yt.utilities.lib.embree_mesh import mesh_traversal # type: ignore