From 6f629fd31af0a85f87aaed1027c1ab1ecf96de35 Mon Sep 17 00:00:00 2001 From: jianfengmao Date: Wed, 6 Mar 2024 15:09:16 -0700 Subject: [PATCH] eval string annotation with fn's globals And remove unnecessary import from __future__ --- py/server/deephaven/_jpy.py | 1 - py/server/deephaven/_udf.py | 19 +++++++++---------- py/server/deephaven/agg.py | 3 +-- py/server/deephaven/execution_context.py | 1 - py/server/deephaven/table_listener.py | 2 -- py/server/deephaven/time.py | 2 -- 6 files changed, 10 insertions(+), 18 deletions(-) diff --git a/py/server/deephaven/_jpy.py b/py/server/deephaven/_jpy.py index 2615872572b..a033ceaf231 100644 --- a/py/server/deephaven/_jpy.py +++ b/py/server/deephaven/_jpy.py @@ -4,7 +4,6 @@ """ This module is an internal module to simplify usage patterns around jpy. """ -from __future__ import annotations import jpy diff --git a/py/server/deephaven/_udf.py b/py/server/deephaven/_udf.py index b6b69479805..787f467a02a 100644 --- a/py/server/deephaven/_udf.py +++ b/py/server/deephaven/_udf.py @@ -2,8 +2,6 @@ # Copyright (c) 2016-2023 Deephaven Data Labs and Patent Pending # -from __future__ import annotations - import inspect import re import sys @@ -113,11 +111,6 @@ def _parse_type_no_nested(annotation: Any, p_param: _ParsedParam, t: Union[type, """ Parse a specific type (top level or nested in a top-level Union annotation) without handling nested types (e.g. a nested Union). The result is stored in the given _ParsedAnnotation object. """ - # when from __future__ import annotations is used, the annotation is a string, we need to eval it to get the type - # when the minimum Python version is bumped to 3.10, we'll always use eval_str in _parse_signature, so that - # annotation is already a type, and we can remove this line. - t = eval(t) if isinstance(t, str) else t - p_param.orig_types.add(t) # if the annotation is a DH DType instance, we'll use its numpy type @@ -260,10 +253,16 @@ def _parse_signature(fn: Callable) -> _ParsedSignature: sig = inspect.signature(fn, eval_str=True) else: sig = inspect.signature(fn) - for n, p in sig.parameters.items(): - p_sig.params.append(_parse_param(n, p.annotation)) - p_sig.ret_annotation = _parse_return_annotation(sig.return_annotation) + for n, p in sig.parameters.items(): + # when from __future__ import annotations is used, the annotation is a string, we need to eval it to get the type + # when the minimum Python version is bumped to 3.10, we'll always use eval_str in _parse_signature, so that + # annotation is already a type, and we can skip this step. + t = eval(p.annotation, fn.__globals__) if isinstance(p.annotation, str) else p.annotation + p_sig.params.append(_parse_param(n, t)) + + t = eval(sig.return_annotation, fn.__globals__) if isinstance(sig.return_annotation, str) else sig.return_annotation + p_sig.ret_annotation = _parse_return_annotation(t) return p_sig diff --git a/py/server/deephaven/agg.py b/py/server/deephaven/agg.py index 565d03c9fe5..97c1d4855d1 100644 --- a/py/server/deephaven/agg.py +++ b/py/server/deephaven/agg.py @@ -1,8 +1,7 @@ # # Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending # - -from __future__ import annotations +"""This module implement various aggregations that can be used in deephaven table's aggregation operations.""" from typing import List, Union, Any diff --git a/py/server/deephaven/execution_context.py b/py/server/deephaven/execution_context.py index 8e416b9e229..87e334d2576 100644 --- a/py/server/deephaven/execution_context.py +++ b/py/server/deephaven/execution_context.py @@ -4,7 +4,6 @@ """This module gives users the ability to directly manage the Deephaven query execution context on threads, which is critical for applications to correctly launch deferred query evaluations, such as table update operations in threads. """ -from __future__ import annotations from typing import Sequence, Union from contextlib import ContextDecorator diff --git a/py/server/deephaven/table_listener.py b/py/server/deephaven/table_listener.py index f0959cb20f4..45f7bd6c239 100644 --- a/py/server/deephaven/table_listener.py +++ b/py/server/deephaven/table_listener.py @@ -1,9 +1,7 @@ # # Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending # - """ This module provides utilities for listening to table changes. """ -from __future__ import annotations from abc import ABC, abstractmethod from functools import wraps diff --git a/py/server/deephaven/time.py b/py/server/deephaven/time.py index 11c8897dfd9..bac848f57e1 100644 --- a/py/server/deephaven/time.py +++ b/py/server/deephaven/time.py @@ -4,8 +4,6 @@ """ This module defines functions for handling Deephaven date/time data. """ -from __future__ import annotations - import datetime from typing import Union, Optional, Literal