type-annotation

How to get Python variable annotations?

自古美人都是妖i 提交于 2021-02-18 21:00:42
问题 When defining a class/module with annotated fields, how can I get annotations as like in functions? class Test: def __init__(self): self.x : int t = Test() Now I need 'int' from getattr(t,'x') 回答1: With baseline Python, there is no option to do what you want without changing the definition of Test . The minimalist change would be to annotate the attribute at class level: class Test: x: int def __init__(self): # define self.x or not, but it needn't be annotated again This is actually perfectly

How do I annotate a type which has an extra attribute with mypy?

你说的曾经没有我的故事 提交于 2021-01-29 03:54:26
问题 I have an ast.UnaryOp object, but I manually added a parent attribute (see answer). How do I annotate this in a function? I currently simply have: def _get_sim206(node: ast.UnaryOp): if isinstance(node.parent, ast.If): return False return True But mypy complains (rightfully so) that ast.UnaryOp does not have the parent attribute. How can I tell mypy that the node is not ast.UnaryOp but ast.UnaryOp + parent attribute ? My Try I've created my own UnaryOp class which has a parent attribute. I

Type annotations for Django models

荒凉一梦 提交于 2020-06-13 23:09:10
问题 I'm working on a Django project. Since this is a new project, I want to have it fully annotated with python 3.6+ type annotations. I'm trying to annotate models, but I struggle to find a good method for that. Let's take the IntegerField as an example. I see two choices for annotating it: # number 1 int_field: int = models.IntegerField() # number 2 int_field: models.IntegerField = models.IntegerField() Number 1 fails in mypy: Incompatible types in assignment (expression has type "IntegerField[

Type annotations for Django models

走远了吗. 提交于 2020-06-13 22:59:51
问题 I'm working on a Django project. Since this is a new project, I want to have it fully annotated with python 3.6+ type annotations. I'm trying to annotate models, but I struggle to find a good method for that. Let's take the IntegerField as an example. I see two choices for annotating it: # number 1 int_field: int = models.IntegerField() # number 2 int_field: models.IntegerField = models.IntegerField() Number 1 fails in mypy: Incompatible types in assignment (expression has type "IntegerField[

Type annotations for Django models

馋奶兔 提交于 2020-06-13 22:59:17
问题 I'm working on a Django project. Since this is a new project, I want to have it fully annotated with python 3.6+ type annotations. I'm trying to annotate models, but I struggle to find a good method for that. Let's take the IntegerField as an example. I see two choices for annotating it: # number 1 int_field: int = models.IntegerField() # number 2 int_field: models.IntegerField = models.IntegerField() Number 1 fails in mypy: Incompatible types in assignment (expression has type "IntegerField[

Type annotations for Django models

旧城冷巷雨未停 提交于 2020-06-13 22:59:08
问题 I'm working on a Django project. Since this is a new project, I want to have it fully annotated with python 3.6+ type annotations. I'm trying to annotate models, but I struggle to find a good method for that. Let's take the IntegerField as an example. I see two choices for annotating it: # number 1 int_field: int = models.IntegerField() # number 2 int_field: models.IntegerField = models.IntegerField() Number 1 fails in mypy: Incompatible types in assignment (expression has type "IntegerField[

Is it possible to access Java 8 type information at runtime?

时间秒杀一切 提交于 2019-12-18 12:54:43
问题 Assuming I have the following member in a class which makes use of Java 8 type annotations: private List<@Email String> emailAddresses; Is it possible to read the @Email annotation given on the String type use at runtime using reflection? If so, how would this be done? Update: That's the definition of the annotation type: @Target(value=ElementType.TYPE_USE) @Retention(RetentionPolicy.RUNTIME) public @interface Email {} 回答1: Yes it is possible. The reflection type representing this kind of

What ASM Visitor Method gets called for type annotation on catch

Deadly 提交于 2019-12-12 01:49:53
问题 I have the following code snippet, which I analyze with ASM try{ } catch (@TypeAnno7 RuntimeException re){ } I can't find the right method that gets called for the Annotation. I thought MethodVisitor.visitTryCatchAnnotation would do the trick, but it doesn't get called. So: what is the correct method used? And for what kind of code does the method mentioned get called? 回答1: The Java compiler does not generate any bytecode for a try/catch unless the try body contains some code (otherwise, the

Python type annotations for Enum value

柔情痞子 提交于 2019-12-09 08:23:35
问题 I have this piece of code: import enum class Color(enum.Enum): RED = '1' BLUE = '2' GREEN = '3' def get_color_return_something(some_color): pass How do I properly add type annotations to the some_color varaible in this function, if I suppose to receive a value from the Color enum (for example: Color.RED )? 回答1: Type hinting the Color class should work: def get_color_return_something(some_color: Color): print(some_color.value) 回答2: def get_color_return_something(some_color: Color): pass 来源:

How to get generic type information from getAnnotatedParameterTypes() in Java 8?

与世无争的帅哥 提交于 2019-12-04 20:36:03
问题 It seems like getAnnotatedParameterTypes() returns an array of AnnotatedType s holding raw, rather than generic, types. For example: public <T> void genericMethod(T t) { } @Test public void testAnnotatedTypes() throws ReflectiveOperationException { Method method = getClass().getMethod("genericMethod", Object.class); Type type = method.getGenericParameterTypes()[0]; assertTrue(type instanceof TypeVariable); AnnotatedType annotatedType = method.getAnnotatedParameterTypes()[0]; // This fails;