Skip to content

Commit

Permalink
If we can't determine whether an annotation is postponed or preevalua…
Browse files Browse the repository at this point in the history
…ted, drop it
  • Loading branch information
epsy committed Oct 12, 2022
1 parent f5d98d4 commit 5995207
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions sigtools/_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import __future__
import abc
import inspect
import sys
import types
from itertools import zip_longest
import itertools
Expand Down Expand Up @@ -61,17 +61,14 @@ def upgrade(cls, raw_annotation, function, param_name, *, _stacklevel=0) -> 'Upg
)
return EmptyAnnotation

feature = getattr(__future__, "annotations", None)
if feature:
try:
has_flag = function.__code__.co_flags & feature.compiler_flag
except AttributeError:
return EmptyAnnotation
else:
if has_flag:
return _PostponedAnnotation(raw_annotation, function)
has_feature = _is_co_flag_enabled(function)

return _PreEvaluatedAnnotation(raw_annotation)
if has_feature is None:
return EmptyAnnotation
elif has_feature:
return _PostponedAnnotation(raw_annotation, function)
else:
return _PreEvaluatedAnnotation(raw_annotation)

@classmethod
def preevaluated(cls, value) -> 'UpgradedAnnotation':
Expand All @@ -87,6 +84,22 @@ def __eq__(self, other):
return False


def _is_co_flag_enabled(obj):
feature = getattr(__future__, "annotations", None)
if not feature:
return False

if feature.mandatory and sys.version_info >= feature.mandatory:
return True

try:
has_flag = obj.__code__.co_flags & feature.compiler_flag
except AttributeError:
return None
else:
return has_flag


@attr.define(eq=False)
class _PostponedAnnotation(UpgradedAnnotation):
"""An annotation whose evaluation was postponed per :PEP:`563`"""
Expand Down

0 comments on commit 5995207

Please sign in to comment.