kandi background
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
Explore all Code Inspection open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Code Inspection

v6.1.8

v1.8.2

Release 1.6.0

Renames, Ranges, and the Copy Command

v0.15.0.1

dnSpy

v6.1.8

delve

v1.8.2

stetho

Release 1.6.0

chisel

Renames, Ranges, and the Copy Command

gdbgui

v0.15.0.1

Popular Libraries in Code Inspection

Trending New libraries in Code Inspection

Top Authors in Code Inspection

1

17 Libraries

7387

2

14 Libraries

1197

3

9 Libraries

1471

4

8 Libraries

191

5

8 Libraries

40661

6

8 Libraries

161

7

8 Libraries

4446

8

7 Libraries

50

9

6 Libraries

191

10

5 Libraries

24

1

17 Libraries

7387

2

14 Libraries

1197

3

9 Libraries

1471

4

8 Libraries

191

5

8 Libraries

40661

6

8 Libraries

161

7

8 Libraries

4446

8

7 Libraries

50

9

6 Libraries

191

10

5 Libraries

24

Trending Kits in Code Inspection

No Trending Kits are available at this moment for Code Inspection

Trending Discussions on Code Inspection

    Expected type 'Type[Add | Sub | Mult | Div | Pow | BitXor | USub]', got 'Type[operator]' instead
    Is it possible to determine which level/key of a nested dict that contained None, causing 'NoneType' object is not subscriptable?
    How does Intellij code inspection decide on selectors from .css files?
    Android studio code inspection shell script for integration in CI pipeline
    ESLint: Cannot start language service process / problem with HLS .ts extension
    Define a classes possible properties
    How do I check ReSharper code inspections of Unity Project via a command line?
    Trouble using std::make_unique with member variable of class
    Contstraint stream breaks when switching from OptaPlanner 7.46.0 to 8.0.0
    jcrop-holder is duplicated on my live page

QUESTION

Expected type 'Type[Add | Sub | Mult | Div | Pow | BitXor | USub]', got 'Type[operator]' instead

Asked 2022-Mar-05 at 00:22
1# Taken out of context for MVCE
2
3import ast
4import operator as op
5
6OPERATORS = {
7    ast.Add: op.add,
8    ast.Sub: op.sub,
9    ast.Mult: op.mul,
10    ast.Div: op.truediv,
11    ast.Pow: op.pow,
12    ast.BitXor: op.xor,
13    ast.USub: op.neg
14}
15
16def eval_expr(expr):
17
18    return eval_(ast.parse(expr, mode='eval').body)
19
20def eval_(node):
21    if isinstance(node, ast.Num):  # <number>
22        value = node.n
23    elif isinstance(node, ast.BinOp):  # <left> <operator> <right>
24        value = OPERATORS[type(node.op)](eval_(node.left), eval_(node.right))
25    elif isinstance(node, ast.UnaryOp):  # <operator> <operand> e.g., -1
26        value = OPERATORS[type(node.op)](eval_(node.operand))
27    else:
28        raise TypeError(node)
29
30    return value
31
32x = eval_expr("1 + 2")
33print(x)
34

PyCharm code inspection highlights the instances of type(node.op) as problematic:

1# Taken out of context for MVCE
2
3import ast
4import operator as op
5
6OPERATORS = {
7    ast.Add: op.add,
8    ast.Sub: op.sub,
9    ast.Mult: op.mul,
10    ast.Div: op.truediv,
11    ast.Pow: op.pow,
12    ast.BitXor: op.xor,
13    ast.USub: op.neg
14}
15
16def eval_expr(expr):
17
18    return eval_(ast.parse(expr, mode='eval').body)
19
20def eval_(node):
21    if isinstance(node, ast.Num):  # <number>
22        value = node.n
23    elif isinstance(node, ast.BinOp):  # <left> <operator> <right>
24        value = OPERATORS[type(node.op)](eval_(node.left), eval_(node.right))
25    elif isinstance(node, ast.UnaryOp):  # <operator> <operand> e.g., -1
26        value = OPERATORS[type(node.op)](eval_(node.operand))
27    else:
28        raise TypeError(node)
29
30    return value
31
32x = eval_expr("1 + 2")
33print(x)
34Expected type 'Type[Add | Sub | Mult | Div | Pow | BitXor | USub]' (matched generic type '_KT'), got 'Type[operator]' instead
35Expected type 'Type[Add | Sub | Mult | Div | Pow | BitXor | USub]' (matched generic type '_KT'), got 'Type[unaryop]' instead
36

The class seems to function just fine, but my OCD wants to know how this could be refactored to avoid the inspection warnings. Or is this a PyCharm inspection gremlin?

ANSWER

Answered 2022-Mar-05 at 00:22

The type checker is warning you that your dictionary that maps AST node types for operators to their implementations is incomplete. The type checker knows all of the possible types of node.op (which it seems to be describing as subtypes of the ast.operator and ast.unaryop parent types), and has noticed that your dictionary doesn't handle them all.

Since there are operators that you haven't included, it's possible for a parsable expression (like, say "2 << 5" which does a left shift, or "~31" which does a bitwise inversion) to fail to be handled by your code.

While I don't use PyCharm and thus can't test it for myself, you can probably satisfy its type checker by adding some error handling to your code, so that operator types you don't support will still be dealt with appropriately, rather than causing an uncaught exception (such as a KeyError from the dictionary) to leak out. For instance, you could use OPERATORS.get(type(node.op)) and then test for None before calling the result. If the operator type isn't in the dictionary, you'd raise an exception of your own.

Source https://stackoverflow.com/questions/71353183

Community Discussions contain sources that include Stack Exchange Network

    Expected type 'Type[Add | Sub | Mult | Div | Pow | BitXor | USub]', got 'Type[operator]' instead
    Is it possible to determine which level/key of a nested dict that contained None, causing 'NoneType' object is not subscriptable?
    How does Intellij code inspection decide on selectors from .css files?
    Android studio code inspection shell script for integration in CI pipeline
    ESLint: Cannot start language service process / problem with HLS .ts extension
    Define a classes possible properties
    How do I check ReSharper code inspections of Unity Project via a command line?
    Trouble using std::make_unique with member variable of class
    Contstraint stream breaks when switching from OptaPlanner 7.46.0 to 8.0.0
    jcrop-holder is duplicated on my live page

QUESTION

Expected type 'Type[Add | Sub | Mult | Div | Pow | BitXor | USub]', got 'Type[operator]' instead

Asked 2022-Mar-05 at 00:22
1# Taken out of context for MVCE
2
3import ast
4import operator as op
5
6OPERATORS = {
7    ast.Add: op.add,
8    ast.Sub: op.sub,
9    ast.Mult: op.mul,
10    ast.Div: op.truediv,
11    ast.Pow: op.pow,
12    ast.BitXor: op.xor,
13    ast.USub: op.neg
14}
15
16def eval_expr(expr):
17
18    return eval_(ast.parse(expr, mode='eval').body)
19
20def eval_(node):
21    if isinstance(node, ast.Num):  # &lt;number&gt;
22        value = node.n
23    elif isinstance(node, ast.BinOp):  # &lt;left&gt; &lt;operator&gt; &lt;right&gt;
24        value = OPERATORS[type(node.op)](eval_(node.left), eval_(node.right))
25    elif isinstance(node, ast.UnaryOp):  # &lt;operator&gt; &lt;operand&gt; e.g., -1
26        value = OPERATORS[type(node.op)](eval_(node.operand))
27    else:
28        raise TypeError(node)
29
30    return value
31
32x = eval_expr(&quot;1 + 2&quot;)
33print(x)
34

PyCharm code inspection highlights the instances of type(node.op) as problematic:

1# Taken out of context for MVCE
2
3import ast
4import operator as op
5
6OPERATORS = {
7    ast.Add: op.add,
8    ast.Sub: op.sub,
9    ast.Mult: op.mul,
10    ast.Div: op.truediv,
11    ast.Pow: op.pow,
12    ast.BitXor: op.xor,
13    ast.USub: op.neg
14}
15
16def eval_expr(expr):
17
18    return eval_(ast.parse(expr, mode='eval').body)
19
20def eval_(node):
21    if isinstance(node, ast.Num):  # &lt;number&gt;
22        value = node.n
23    elif isinstance(node, ast.BinOp):  # &lt;left&gt; &lt;operator&gt; &lt;right&gt;
24        value = OPERATORS[type(node.op)](eval_(node.left), eval_(node.right))
25    elif isinstance(node, ast.UnaryOp):  # &lt;operator&gt; &lt;operand&gt; e.g., -1
26        value = OPERATORS[type(node.op)](eval_(node.operand))
27    else:
28        raise TypeError(node)
29
30    return value
31
32x = eval_expr(&quot;1 + 2&quot;)
33print(x)
34Expected type 'Type[Add | Sub | Mult | Div | Pow | BitXor | USub]' (matched generic type '_KT'), got 'Type[operator]' instead
35Expected type 'Type[Add | Sub | Mult | Div | Pow | BitXor | USub]' (matched generic type '_KT'), got 'Type[unaryop]' instead
36

The class seems to function just fine, but my OCD wants to know how this could be refactored to avoid the inspection warnings. Or is this a PyCharm inspection gremlin?

ANSWER

Answered 2022-Mar-05 at 00:22

The type checker is warning you that your dictionary that maps AST node types for operators to their implementations is incomplete. The type checker knows all of the possible types of node.op (which it seems to be describing as subtypes of the ast.operator and ast.unaryop parent types), and has noticed that your dictionary doesn't handle them all.

Since there are operators that you haven't included, it's possible for a parsable expression (like, say "2 << 5" which does a left shift, or "~31" which does a bitwise inversion) to fail to be handled by your code.

While I don't use PyCharm and thus can't test it for myself, you can probably satisfy its type checker by adding some error handling to your code, so that operator types you don't support will still be dealt with appropriately, rather than causing an uncaught exception (such as a KeyError from the dictionary) to leak out. For instance, you could use OPERATORS.get(type(node.op)) and then test for None before calling the result. If the operator type isn't in the dictionary, you'd raise an exception of your own.

Source https://stackoverflow.com/questions/71353183

Community Discussions contain sources that include Stack Exchange Network

Tutorials and Learning Resources in Code Inspection

Tutorials and Learning Resources are not available at this moment for Code Inspection

Share this Page

share link

Get latest updates on Code Inspection

  • © 2022 Open Weaver Inc.