From f2de9c36fb2f3d0acb26440cd246f441270b05a4 Mon Sep 17 00:00:00 2001 From: Patrick Arminio Date: Thu, 22 Oct 2020 01:18:36 +0100 Subject: [PATCH 1/5] Add failing test --- test-data/unit/check-annotated.test | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test-data/unit/check-annotated.test b/test-data/unit/check-annotated.test index aeb1a1985e6d..58dc33460cc0 100644 --- a/test-data/unit/check-annotated.test +++ b/test-data/unit/check-annotated.test @@ -116,3 +116,13 @@ Alias = Annotated[Union[T, str], ...] x: Alias[int] reveal_type(x) # N: Revealed type is 'Union[builtins.int, builtins.str]' [builtins fixtures/tuple.pyi] + +[case testAnnotatedSecondParamNonType] +from typing_extensions import Annotated + +class Meta: + ... + +x = Annotated[int, Meta()] +reveal_type(x) # N: Revealed type is 'def () -> builtins.int' +[builtins fixtures/tuple.pyi] From c2358cbccb064f69b2c69d93a38aa86435baac4f Mon Sep 17 00:00:00 2001 From: Patrick Arminio Date: Thu, 22 Oct 2020 01:19:01 +0100 Subject: [PATCH 2/5] Don't try to convert annotated's second param to type --- mypy/exprtotype.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mypy/exprtotype.py b/mypy/exprtotype.py index a3b762a1b64f..6ac09c28d8ab 100644 --- a/mypy/exprtotype.py +++ b/mypy/exprtotype.py @@ -61,7 +61,10 @@ def expr_to_unanalyzed_type(expr: Expression, _parent: Optional[Expression] = No args = expr.index.items else: args = [expr.index] - base.args = tuple(expr_to_unanalyzed_type(arg, expr) for arg in args) + if base.name == 'Annotated': + base.args = (expr_to_unanalyzed_type(args[0], expr), args[1]) + else: + base.args = tuple(expr_to_unanalyzed_type(arg, expr) for arg in args) if not base.args: base.empty_tuple_index = True return base From da5e9f69dedc4643ff0be9c9c51af611003fd2d7 Mon Sep 17 00:00:00 2001 From: Patrick Arminio Date: Thu, 22 Oct 2020 01:23:50 +0100 Subject: [PATCH 3/5] Use fullname to check if the current expression is Annotated --- mypy/exprtotype.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/exprtotype.py b/mypy/exprtotype.py index 6ac09c28d8ab..3b4e2ef0a7ca 100644 --- a/mypy/exprtotype.py +++ b/mypy/exprtotype.py @@ -61,7 +61,7 @@ def expr_to_unanalyzed_type(expr: Expression, _parent: Optional[Expression] = No args = expr.index.items else: args = [expr.index] - if base.name == 'Annotated': + if expr.base.fullname in ['typing.Annotated', 'typing_extensions.Annotated']: base.args = (expr_to_unanalyzed_type(args[0], expr), args[1]) else: base.args = tuple(expr_to_unanalyzed_type(arg, expr) for arg in args) From 8ac16943e160e8678844ebd20c6a6447a5a8aed4 Mon Sep 17 00:00:00 2001 From: Patrick Arminio Date: Thu, 22 Oct 2020 01:57:57 +0100 Subject: [PATCH 4/5] Improve check for annotation --- mypy/exprtotype.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mypy/exprtotype.py b/mypy/exprtotype.py index 3b4e2ef0a7ca..9187c13999ab 100644 --- a/mypy/exprtotype.py +++ b/mypy/exprtotype.py @@ -3,7 +3,7 @@ from typing import Optional from mypy.nodes import ( - Expression, NameExpr, MemberExpr, IndexExpr, TupleExpr, IntExpr, FloatExpr, UnaryExpr, + Expression, NameExpr, MemberExpr, IndexExpr, RefExpr, TupleExpr, IntExpr, FloatExpr, UnaryExpr, ComplexExpr, ListExpr, StrExpr, BytesExpr, UnicodeExpr, EllipsisExpr, CallExpr, get_member_expr_fullname ) @@ -61,7 +61,10 @@ def expr_to_unanalyzed_type(expr: Expression, _parent: Optional[Expression] = No args = expr.index.items else: args = [expr.index] - if expr.base.fullname in ['typing.Annotated', 'typing_extensions.Annotated']: + + if isinstance(expr.base, RefExpr) and expr.base.fullname in [ + 'typing.Annotated', 'typing_extensions.Annotated' + ]: base.args = (expr_to_unanalyzed_type(args[0], expr), args[1]) else: base.args = tuple(expr_to_unanalyzed_type(arg, expr) for arg in args) From 32e2beb84ebbdcd315a4a1c0ae1150850df6f35a Mon Sep 17 00:00:00 2001 From: Patrick Arminio Date: Thu, 22 Oct 2020 02:00:04 +0100 Subject: [PATCH 5/5] Only return the annotation's type --- mypy/exprtotype.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mypy/exprtotype.py b/mypy/exprtotype.py index 9187c13999ab..578080477e0c 100644 --- a/mypy/exprtotype.py +++ b/mypy/exprtotype.py @@ -65,7 +65,11 @@ def expr_to_unanalyzed_type(expr: Expression, _parent: Optional[Expression] = No if isinstance(expr.base, RefExpr) and expr.base.fullname in [ 'typing.Annotated', 'typing_extensions.Annotated' ]: - base.args = (expr_to_unanalyzed_type(args[0], expr), args[1]) + # TODO: this is not the optimal solution as we are basically getting rid + # of the Annotation definition and only returning the type information, + # losing all the annotations. + + return expr_to_unanalyzed_type(args[0], expr) else: base.args = tuple(expr_to_unanalyzed_type(arg, expr) for arg in args) if not base.args: pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy