Author douglas-raillard-arm
Recipients douglas-raillard-arm, rhettinger
Date 2021-02-03.10:16:58
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1612347419.45.0.310069573601.issue43102@roundup.psfhosted.org>
In-reply-to
Content
I did hit the issue while porting a tool to Python 3.9:
https://github.com/ARM-software/lisa/pull/1585/commits/a4cd3aa1ad339ebfe59cc9e2ae290bb3788c900d

It basically infers valid Python expressions from type annotations (some sort
of inverse type checker) and run them while serializing all intermediate
subexpressions for debugging. This allows a whole test suite to be plain
classes and functions (usable out of context) to be assembled into "pipelines"
without extra user work. The reason I ran into that problem are:

1. I'm scanning whole modules for annotations, so it is exposed to lots of things
2. In order to be able to infer expressions, it has to augment existing
  annotations when it is unambiguous. In our case, since __new__ is more or
  less a classmethod (i.e. it takes the class as first argument even if
  strictly speaking it's a staticmethod), it added an annotation with the class
  name (extracted from __qualname__).

It implements PEP563 to evaluate the annotions, which describes how to get the
globals for a function and class and that they can be fed to eval(). Using
typing.get_type_hints() is not really possible since annotations need to be
augmented, and fancy type hints like Optional are unusable for my purpose since
there typing.get_args() was only added to Python 3.8 (I need to support >=
3.6).

Maybe an alternative would be to use a module-level types.MappingProxyType
instance ? This way there is no extra object created for each namedtuple and
since it's read-only it should be as safe as None.
History
Date User Action Args
2021-02-03 10:16:59douglas-raillard-armsetrecipients: + douglas-raillard-arm, rhettinger
2021-02-03 10:16:59douglas-raillard-armsetmessageid: <1612347419.45.0.310069573601.issue43102@roundup.psfhosted.org>
2021-02-03 10:16:59douglas-raillard-armlinkissue43102 messages
2021-02-03 10:16:58douglas-raillard-armcreate