From cbd3fe3706de6eef2fb3e703aeb2a80b8bc81789 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 6 Aug 2021 13:20:11 +0200 Subject: [PATCH 01/19] Add typing to the OPTIONS dict --- xarray/core/options.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index 7104e12c29f..5066bcb6bcf 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -1,4 +1,5 @@ import warnings +from typing import TypedDict ARITHMETIC_JOIN = "arithmetic_join" CMAP_DIVERGENT = "cmap_divergent" @@ -16,7 +17,24 @@ WARN_FOR_UNCLOSED_FILES = "warn_for_unclosed_files" -OPTIONS = { +class TypedOptions(TypedDict): + ARITHMETIC_JOIN: str + CMAP_DIVERGENT: str + CMAP_SEQUENTIAL: str + DISPLAY_MAX_ROWS: int + DISPLAY_STYLE: str + DISPLAY_WIDTH: int + DISPLAY_EXPAND_ATTRS: str + DISPLAY_EXPAND_COORDS: str + DISPLAY_EXPAND_DATA_VARS: str + DISPLAY_EXPAND_DATA: str + ENABLE_CFTIMEINDEX: bool + FILE_CACHE_MAXSIZE: int + KEEP_ATTRS: str + WARN_FOR_UNCLOSED_FILES: bool + + +OPTIONS: TypedOptions = { ARITHMETIC_JOIN: "inner", CMAP_DIVERGENT: "RdBu_r", CMAP_SEQUENTIAL: "viridis", From 729fe0f44caf341dff3740c92fa17d25403f970e Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 6 Aug 2021 13:39:49 +0200 Subject: [PATCH 02/19] Update options.py --- xarray/core/options.py | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index 5066bcb6bcf..27bb72ccf6d 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -17,24 +17,28 @@ WARN_FOR_UNCLOSED_FILES = "warn_for_unclosed_files" -class TypedOptions(TypedDict): - ARITHMETIC_JOIN: str - CMAP_DIVERGENT: str - CMAP_SEQUENTIAL: str - DISPLAY_MAX_ROWS: int - DISPLAY_STYLE: str - DISPLAY_WIDTH: int - DISPLAY_EXPAND_ATTRS: str - DISPLAY_EXPAND_COORDS: str - DISPLAY_EXPAND_DATA_VARS: str - DISPLAY_EXPAND_DATA: str - ENABLE_CFTIMEINDEX: bool - FILE_CACHE_MAXSIZE: int - KEEP_ATTRS: str - WARN_FOR_UNCLOSED_FILES: bool - - -OPTIONS: TypedOptions = { +T_Options = TypedDict( + "T_Options", + { + ARITHMETIC_JOIN: str, + CMAP_DIVERGENT: str, + CMAP_SEQUENTIAL: str, + DISPLAY_MAX_ROWS: int, + DISPLAY_STYLE: str, + DISPLAY_WIDTH: int, + DISPLAY_EXPAND_ATTRS: str, + DISPLAY_EXPAND_COORDS: str, + DISPLAY_EXPAND_DATA_VARS: str, + DISPLAY_EXPAND_DATA: str, + ENABLE_CFTIMEINDEX: bool, + FILE_CACHE_MAXSIZE: int, + KEEP_ATTRS: str, + WARN_FOR_UNCLOSED_FILES: bool, + }, +) + + +OPTIONS: T_Options = { ARITHMETIC_JOIN: "inner", CMAP_DIVERGENT: "RdBu_r", CMAP_SEQUENTIAL: "viridis", From 475c42ba993fac8aa3d1f5574b87a8792bf5c75a Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 6 Aug 2021 13:59:06 +0200 Subject: [PATCH 03/19] Update options.py --- xarray/core/options.py | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index 27bb72ccf6d..cd77e1318f7 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -17,25 +17,22 @@ WARN_FOR_UNCLOSED_FILES = "warn_for_unclosed_files" -T_Options = TypedDict( - "T_Options", - { - ARITHMETIC_JOIN: str, - CMAP_DIVERGENT: str, - CMAP_SEQUENTIAL: str, - DISPLAY_MAX_ROWS: int, - DISPLAY_STYLE: str, - DISPLAY_WIDTH: int, - DISPLAY_EXPAND_ATTRS: str, - DISPLAY_EXPAND_COORDS: str, - DISPLAY_EXPAND_DATA_VARS: str, - DISPLAY_EXPAND_DATA: str, - ENABLE_CFTIMEINDEX: bool, - FILE_CACHE_MAXSIZE: int, - KEEP_ATTRS: str, - WARN_FOR_UNCLOSED_FILES: bool, - }, -) +class T_Options(TypedDict): + # Can't use the variables here unfortunately: + arithmetic_join: str + cmap_divergent: str + cmap_sequential: str + display_max_rows: int + display_style: str + display_width: int + display_expand_attrs: str + display_expand_coords: str + display_expand_data_vars: str + display_expand_data: str + enable_cftimeindex: bool + file_cache_maxsize: int + keep_attrs: str + warn_for_unclosed_files: bool OPTIONS: T_Options = { From 2fbe3edc3e6579f717b1a4fb5e395e5452fa928e Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 6 Aug 2021 14:17:17 +0200 Subject: [PATCH 04/19] Don't use variables as keys --- xarray/core/options.py | 75 +++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 45 deletions(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index cd77e1318f7..8b7c04505d7 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -1,21 +1,6 @@ import warnings from typing import TypedDict -ARITHMETIC_JOIN = "arithmetic_join" -CMAP_DIVERGENT = "cmap_divergent" -CMAP_SEQUENTIAL = "cmap_sequential" -DISPLAY_MAX_ROWS = "display_max_rows" -DISPLAY_STYLE = "display_style" -DISPLAY_WIDTH = "display_width" -DISPLAY_EXPAND_ATTRS = "display_expand_attrs" -DISPLAY_EXPAND_COORDS = "display_expand_coords" -DISPLAY_EXPAND_DATA_VARS = "display_expand_data_vars" -DISPLAY_EXPAND_DATA = "display_expand_data" -ENABLE_CFTIMEINDEX = "enable_cftimeindex" -FILE_CACHE_MAXSIZE = "file_cache_maxsize" -KEEP_ATTRS = "keep_attrs" -WARN_FOR_UNCLOSED_FILES = "warn_for_unclosed_files" - class T_Options(TypedDict): # Can't use the variables here unfortunately: @@ -36,20 +21,20 @@ class T_Options(TypedDict): OPTIONS: T_Options = { - ARITHMETIC_JOIN: "inner", - CMAP_DIVERGENT: "RdBu_r", - CMAP_SEQUENTIAL: "viridis", - DISPLAY_MAX_ROWS: 12, - DISPLAY_STYLE: "html", - DISPLAY_WIDTH: 80, - DISPLAY_EXPAND_ATTRS: "default", - DISPLAY_EXPAND_COORDS: "default", - DISPLAY_EXPAND_DATA_VARS: "default", - DISPLAY_EXPAND_DATA: "default", - ENABLE_CFTIMEINDEX: True, - FILE_CACHE_MAXSIZE: 128, - KEEP_ATTRS: "default", - WARN_FOR_UNCLOSED_FILES: False, + "arithmetic_join": "inner", + "cmap_divergent": "RdBu_r", + "cmap_sequential": "viridis", + "display_max_rows": 12, + "display_style": "html", + "display_width": 80, + "display_expand_attrs": "default", + "display_expand_coords": "default", + "display_expand_data_vars": "default", + "display_expand_data": "default", + "enable_cftimeindex": True, + "file_cache_maxsize": 128, + "keep_attrs": "default", + "warn_for_unclosed_files": False, } _JOIN_OPTIONS = frozenset(["inner", "outer", "left", "right", "exact"]) @@ -61,18 +46,18 @@ def _positive_integer(value): _VALIDATORS = { - ARITHMETIC_JOIN: _JOIN_OPTIONS.__contains__, - DISPLAY_MAX_ROWS: _positive_integer, - DISPLAY_STYLE: _DISPLAY_OPTIONS.__contains__, - DISPLAY_WIDTH: _positive_integer, - DISPLAY_EXPAND_ATTRS: lambda choice: choice in [True, False, "default"], - DISPLAY_EXPAND_COORDS: lambda choice: choice in [True, False, "default"], - DISPLAY_EXPAND_DATA_VARS: lambda choice: choice in [True, False, "default"], - DISPLAY_EXPAND_DATA: lambda choice: choice in [True, False, "default"], - ENABLE_CFTIMEINDEX: lambda value: isinstance(value, bool), - FILE_CACHE_MAXSIZE: _positive_integer, - KEEP_ATTRS: lambda choice: choice in [True, False, "default"], - WARN_FOR_UNCLOSED_FILES: lambda value: isinstance(value, bool), + "arithmetic_join": _JOIN_OPTIONS.__contains__, + "display_max_rows": _positive_integer, + "display_style": _DISPLAY_OPTIONS.__contains__, + "display_width": _positive_integer, + "display_expand_attrs": lambda choice: choice in [True, False, "default"], + "display_expand_coords": lambda choice: choice in [True, False, "default"], + "display_expand_data_vars": lambda choice: choice in [True, False, "default"], + "display_expand_data": lambda choice: choice in [True, False, "default"], + "enable_cftimeindex": lambda value: isinstance(value, bool), + "file_cache_maxsize": _positive_integer, + "keep_attrs": lambda choice: choice in [True, False, "default"], + "warn_for_unclosed_files": lambda value: isinstance(value, bool), } @@ -91,8 +76,8 @@ def _warn_on_setting_enable_cftimeindex(enable_cftimeindex): _SETTERS = { - ENABLE_CFTIMEINDEX: _warn_on_setting_enable_cftimeindex, - FILE_CACHE_MAXSIZE: _set_file_cache_maxsize, + "enable_cftimeindex": _warn_on_setting_enable_cftimeindex, + "file_cache_maxsize": _set_file_cache_maxsize, } @@ -188,9 +173,9 @@ def __init__(self, **kwargs): f"argument name {k!r} is not in the set of valid options {set(OPTIONS)!r}" ) if k in _VALIDATORS and not _VALIDATORS[k](v): - if k == ARITHMETIC_JOIN: + if k == "arithmetic_join": expected = f"Expected one of {_JOIN_OPTIONS!r}" - elif k == DISPLAY_STYLE: + elif k == "display_style": expected = f"Expected one of {_DISPLAY_OPTIONS!r}" else: expected = "" From d278a50bc95b2601b46435c25f776fb73e424bdc Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 6 Aug 2021 14:20:15 +0200 Subject: [PATCH 05/19] Update options.py --- xarray/core/options.py | 1 - 1 file changed, 1 deletion(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index 8b7c04505d7..9b48647b571 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -3,7 +3,6 @@ class T_Options(TypedDict): - # Can't use the variables here unfortunately: arithmetic_join: str cmap_divergent: str cmap_sequential: str From f72df8d5db9e760529fecc276c6277f8fd90c105 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 6 Aug 2021 14:30:06 +0200 Subject: [PATCH 06/19] backwards compatibility --- xarray/core/options.py | 46 +++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index 9b48647b571..3d634822244 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -1,22 +1,32 @@ import warnings -from typing import TypedDict - - -class T_Options(TypedDict): - arithmetic_join: str - cmap_divergent: str - cmap_sequential: str - display_max_rows: int - display_style: str - display_width: int - display_expand_attrs: str - display_expand_coords: str - display_expand_data_vars: str - display_expand_data: str - enable_cftimeindex: bool - file_cache_maxsize: int - keep_attrs: str - warn_for_unclosed_files: bool +import sys + + +# TODO: Remove this check once python 3.7 is not supported: +if sys.version_info >= (3, 7): + from typing import TypedDict + + class T_Options(TypedDict): + arithmetic_join: str + cmap_divergent: str + cmap_sequential: str + display_max_rows: int + display_style: str + display_width: int + display_expand_attrs: str + display_expand_coords: str + display_expand_data_vars: str + display_expand_data: str + enable_cftimeindex: bool + file_cache_maxsize: int + keep_attrs: str + warn_for_unclosed_files: bool + + +else: + from typing import Dict, Hashable, Any + + T_Options = Dict[Hashable, Any] OPTIONS: T_Options = { From b0bd433c67b68e27218d459c074e973bc1f7c2ca Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 6 Aug 2021 14:31:44 +0200 Subject: [PATCH 07/19] Update options.py --- xarray/core/options.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index 3d634822244..d58958a9ecc 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -3,7 +3,7 @@ # TODO: Remove this check once python 3.7 is not supported: -if sys.version_info >= (3, 7): +if sys.version_info > (3, 7): from typing import TypedDict class T_Options(TypedDict): From 9e8897ed5103d5755e7e59b17646d12fb84b9217 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 6 Aug 2021 14:35:30 +0200 Subject: [PATCH 08/19] Update options.py --- xarray/core/options.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index d58958a9ecc..b6837642678 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -1,5 +1,5 @@ -import warnings import sys +import warnings # TODO: Remove this check once python 3.7 is not supported: @@ -24,7 +24,7 @@ class T_Options(TypedDict): else: - from typing import Dict, Hashable, Any + from typing import Any, Dict, Hashable T_Options = Dict[Hashable, Any] From e150327841d004038e2c35e791765988c3bef241 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 6 Aug 2021 15:29:58 +0200 Subject: [PATCH 09/19] Update options.py --- xarray/core/options.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index b6837642678..be7990e23f5 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -1,9 +1,8 @@ import sys import warnings - # TODO: Remove this check once python 3.7 is not supported: -if sys.version_info > (3, 7): +if sys.version_info >= (3, 8): from typing import TypedDict class T_Options(TypedDict): From c0f64b3450682d87864fd0e3de614c0bde2e09de Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 6 Aug 2021 23:40:54 +0200 Subject: [PATCH 10/19] get TypedDict from typing_extensions --- xarray/core/options.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index be7990e23f5..e97a380bba8 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -23,9 +23,32 @@ class T_Options(TypedDict): else: - from typing import Any, Dict, Hashable - - T_Options = Dict[Hashable, Any] + # See GH5624, this is a convoluted way to allow type-checking to use + # `TypedDict` without requiring typing_extensions as a required dependency + # to _run_ the code (it is required to type-check). + try: + from typing_extensions import TypedDict + + class T_Options(TypedDict): + arithmetic_join: str + cmap_divergent: str + cmap_sequential: str + display_max_rows: int + display_style: str + display_width: int + display_expand_attrs: str + display_expand_coords: str + display_expand_data_vars: str + display_expand_data: str + enable_cftimeindex: bool + file_cache_maxsize: int + keep_attrs: str + warn_for_unclosed_files: bool + + except ImportError: + from typing import Any, Dict, Hashable + + T_Options = Dict[Hashable, Any] OPTIONS: T_Options = { From 63efccc16133083f8280bdc912aae89b3834719d Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 6 Aug 2021 23:53:36 +0200 Subject: [PATCH 11/19] Update options.py --- xarray/core/options.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index e97a380bba8..b25b17d97d6 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -1,5 +1,6 @@ import sys import warnings +from typing import TYPE_CHECKING # TODO: Remove this check once python 3.7 is not supported: if sys.version_info >= (3, 8): @@ -46,9 +47,12 @@ class T_Options(TypedDict): warn_for_unclosed_files: bool except ImportError: - from typing import Any, Dict, Hashable + if TYPE_CHECKING: + raise + else: + from typing import Any, Dict, Hashable - T_Options = Dict[Hashable, Any] + T_Options = Dict[Hashable, Any] OPTIONS: T_Options = { From daa89183e740ba5d435dae2561e10dee5a26bf5f Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 11 Aug 2021 12:47:52 +0200 Subject: [PATCH 12/19] minor cleanup --- xarray/core/options.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index b25b17d97d6..1c652b128b3 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -1,6 +1,5 @@ import sys import warnings -from typing import TYPE_CHECKING # TODO: Remove this check once python 3.7 is not supported: if sys.version_info >= (3, 8): @@ -47,11 +46,11 @@ class T_Options(TypedDict): warn_for_unclosed_files: bool except ImportError: + from typing import Any, Dict, Hashable, TYPE_CHECKING + if TYPE_CHECKING: raise else: - from typing import Any, Dict, Hashable - T_Options = Dict[Hashable, Any] From 7f437908523b3d271883fbadf226e460a76de35d Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Wed, 11 Aug 2021 12:51:25 +0200 Subject: [PATCH 13/19] Update options.py --- xarray/core/options.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index 1c652b128b3..cc6d8322490 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -46,7 +46,7 @@ class T_Options(TypedDict): warn_for_unclosed_files: bool except ImportError: - from typing import Any, Dict, Hashable, TYPE_CHECKING + from typing import TYPE_CHECKING, Any, Dict, Hashable if TYPE_CHECKING: raise From d2473cd92ff02e4735e80852fe7ed0809a54fcfa Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 13 Aug 2021 10:01:10 +0200 Subject: [PATCH 14/19] Add Colormap as allowed type --- xarray/core/options.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index af447f7a7e9..75c82877269 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -3,12 +3,19 @@ # TODO: Remove this check once python 3.7 is not supported: if sys.version_info >= (3, 8): - from typing import TypedDict + from typing import TYPE_CHECKING, Union + from typing_extensions import TypedDict + + if TYPE_CHECKING: + try: + from matplotlib.colors import Colormap + except ImportError: + Colormap = str class T_Options(TypedDict): arithmetic_join: str - cmap_divergent: str - cmap_sequential: str + cmap_divergent: Union[str, "Colormap"] + cmap_sequential: Union[str, "Colormap"] display_max_rows: int display_style: str display_width: int @@ -28,12 +35,19 @@ class T_Options(TypedDict): # `TypedDict` without requiring typing_extensions as a required dependency # to _run_ the code (it is required to type-check). try: + from typing import TYPE_CHECKING, Union from typing_extensions import TypedDict + if TYPE_CHECKING: + try: + from matplotlib.colors import Colormap + except ImportError: + Colormap = str + class T_Options(TypedDict): arithmetic_join: str - cmap_divergent: str - cmap_sequential: str + cmap_divergent: Union[str, "Colormap"] + cmap_sequential: Union[str, "Colormap"] display_max_rows: int display_style: str display_width: int From 3f8d5eb97417aa4fad6b38345cd079e452274715 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 13 Aug 2021 10:12:19 +0200 Subject: [PATCH 15/19] linting, keep_attrs can be str or bool --- xarray/core/options.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index 75c82877269..56bd3a3aca6 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -4,6 +4,7 @@ # TODO: Remove this check once python 3.7 is not supported: if sys.version_info >= (3, 8): from typing import TYPE_CHECKING, Union + from typing_extensions import TypedDict if TYPE_CHECKING: @@ -25,7 +26,7 @@ class T_Options(TypedDict): display_expand_data: str enable_cftimeindex: bool file_cache_maxsize: int - keep_attrs: str + keep_attrs: Union[str, bool] warn_for_unclosed_files: bool use_bottleneck: bool @@ -36,6 +37,7 @@ class T_Options(TypedDict): # to _run_ the code (it is required to type-check). try: from typing import TYPE_CHECKING, Union + from typing_extensions import TypedDict if TYPE_CHECKING: @@ -57,7 +59,7 @@ class T_Options(TypedDict): display_expand_data: str enable_cftimeindex: bool file_cache_maxsize: int - keep_attrs: str + keep_attrs: Union[str, bool] warn_for_unclosed_files: bool use_bottleneck: bool From 98ad3e96ae7cabd5aacaebe99be93fb46303f7e2 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Fri, 13 Aug 2021 11:13:45 +0200 Subject: [PATCH 16/19] display_expand* can be string or bool --- xarray/core/options.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index 56bd3a3aca6..d8606bbd1d5 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -20,10 +20,10 @@ class T_Options(TypedDict): display_max_rows: int display_style: str display_width: int - display_expand_attrs: str - display_expand_coords: str - display_expand_data_vars: str - display_expand_data: str + display_expand_attrs: Union[str, bool] + display_expand_coords: Union[str, bool] + display_expand_data_vars: Union[str, bool] + display_expand_data: Union[str, bool] enable_cftimeindex: bool file_cache_maxsize: int keep_attrs: Union[str, bool] @@ -53,10 +53,10 @@ class T_Options(TypedDict): display_max_rows: int display_style: str display_width: int - display_expand_attrs: str - display_expand_coords: str - display_expand_data_vars: str - display_expand_data: str + display_expand_attrs: Union[str, bool] + display_expand_coords: Union[str, bool] + display_expand_data_vars: Union[str, bool] + display_expand_data: Union[str, bool] enable_cftimeindex: bool file_cache_maxsize: int keep_attrs: Union[str, bool] From 3fa7f05fb6570a9299966a682115c380b7cc1cd7 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 14 Aug 2021 22:47:59 +0200 Subject: [PATCH 17/19] TypedDict is in typing after 3.8 --- xarray/core/options.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index 110a72bac54..cb95cfe0d2a 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -3,9 +3,7 @@ # TODO: Remove this check once python 3.7 is not supported: if sys.version_info >= (3, 8): - from typing import TYPE_CHECKING, Union - - from typing_extensions import TypedDict + from typing import TYPE_CHECKING, TypedDict, Union if TYPE_CHECKING: try: From d8dc5c68abde42f2eaf59787ee87f2e338bd6e79 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 14 Aug 2021 23:32:03 +0200 Subject: [PATCH 18/19] Try out Literals for better type narrowing --- xarray/core/options.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index cb95cfe0d2a..0ff74126e9e 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -3,7 +3,7 @@ # TODO: Remove this check once python 3.7 is not supported: if sys.version_info >= (3, 8): - from typing import TYPE_CHECKING, TypedDict, Union + from typing import TYPE_CHECKING, Literal, TypedDict, Union if TYPE_CHECKING: try: @@ -12,19 +12,19 @@ Colormap = str class T_Options(TypedDict): - arithmetic_join: str + arithmetic_join: Literal["inner", "outer", "left", "right", "exact"] cmap_divergent: Union[str, "Colormap"] cmap_sequential: Union[str, "Colormap"] display_max_rows: int - display_style: str + display_style: Literal["text", "html"] display_width: int - display_expand_attrs: Union[str, bool] - display_expand_coords: Union[str, bool] - display_expand_data_vars: Union[str, bool] - display_expand_data: Union[str, bool] + display_expand_attrs: Literal["default", True, False] + display_expand_coords: Literal["default", True, False] + display_expand_data_vars: Literal["default", True, False] + display_expand_data: Literal["default", True, False] enable_cftimeindex: bool file_cache_maxsize: int - keep_attrs: Union[str, bool] + keep_attrs: Literal["default", True, False] warn_for_unclosed_files: bool use_bottleneck: bool @@ -34,7 +34,7 @@ class T_Options(TypedDict): # `TypedDict` without requiring typing_extensions as a required dependency # to _run_ the code (it is required to type-check). try: - from typing import TYPE_CHECKING, Union + from typing import TYPE_CHECKING, Literal, Union from typing_extensions import TypedDict @@ -45,19 +45,19 @@ class T_Options(TypedDict): Colormap = str class T_Options(TypedDict): - arithmetic_join: str + arithmetic_join: Literal["inner", "outer", "left", "right", "exact"] cmap_divergent: Union[str, "Colormap"] cmap_sequential: Union[str, "Colormap"] display_max_rows: int - display_style: str + display_style: Literal["text", "html"] display_width: int - display_expand_attrs: Union[str, bool] - display_expand_coords: Union[str, bool] - display_expand_data_vars: Union[str, bool] - display_expand_data: Union[str, bool] + display_expand_attrs: Literal["default", True, False] + display_expand_coords: Literal["default", True, False] + display_expand_data_vars: Literal["default", True, False] + display_expand_data: Literal["default", True, False] enable_cftimeindex: bool file_cache_maxsize: int - keep_attrs: Union[str, bool] + keep_attrs: Literal["default", True, False] warn_for_unclosed_files: bool use_bottleneck: bool From c11aac2f00edf3584d1eec508fc850fb8f17e084 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Sat, 14 Aug 2021 23:58:06 +0200 Subject: [PATCH 19/19] Literal was added python 3.8 --- xarray/core/options.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/xarray/core/options.py b/xarray/core/options.py index 0ff74126e9e..524362ce924 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -31,12 +31,12 @@ class T_Options(TypedDict): else: # See GH5624, this is a convoluted way to allow type-checking to use - # `TypedDict` without requiring typing_extensions as a required dependency - # to _run_ the code (it is required to type-check). + # `TypedDict` and `Literal` without requiring typing_extensions as a + # required dependency to _run_ the code (it is required to type-check). try: - from typing import TYPE_CHECKING, Literal, Union + from typing import TYPE_CHECKING, Union - from typing_extensions import TypedDict + from typing_extensions import Literal, TypedDict if TYPE_CHECKING: try: