You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I presume this is intended to work generally for any expression that involves these types, not only expressions that are used as arguments to reveal_type calls? In other words, do you expect this to work?
a: inta=1+2reveal_type(a) # Literal[3]
You might want to consider adding 'not' to the list of supported operators for bool and '%' and the unary form of '+' and '-' to the list of operations for 'int'.
For int operations, you'll need to decide what to do about numeric overflows and divide-by-zero conditions. Probably OK to fall back on int.
I think this is a good feature idea, so I implemented it in pyright. If you haven't already completed your PR for mypy, perhaps I can save you some time by sharing the unit tests I wrote.
You might want to consider adding 'not' to the list of supported operators for bool and '%' and the unary form of '+' and '-' to the list of operations for 'int'.
Thanks! I haven't touched unary operations right now. But, they are on the list!
For int operations, you'll need to decide what to do about numeric overflows and divide-by-zero conditions. Probably OK to fall back on int.
Yes, this is a plan. In case of any problems / errors we escape from Literal special path to ordinary check_op
I think this is a good feature idea, so I implemented it in pyright. If you haven't already completed your PR for mypy, perhaps I can save you some time by sharing the unit tests I wrote.
One challenge to consider... If you're evaluating types in a loop, you need to be careful about literal math.
Yes, looks like a good unit-test.
Out of curiosity, are you planning to support unions?
They are not supported right now. Probably in the future 🙂
Could * potentially be a supported operation for strings? 'foo' * 2 seems pretty similar to 'foo' + 'foo'.
Yes, I also tought about that. But, right now the logic is that type never changes for both operands. str * int produces str which is the corner case. I will try it!
EDIT: This is far too much unneeded work, but I was thinking you can probably handle these cases without bailing to int or str if you avoid eager evaluation and instead put the corresponding problem into a SMT solver when you need to type check against a literal. 😄
Activity
erictraut commentedon Jan 13, 2022
I presume this is intended to work generally for any expression that involves these types, not only expressions that are used as arguments to
reveal_typecalls? In other words, do you expect this to work?You might want to consider adding
'not'to the list of supported operators forbooland'%'and the unary form of'+'and'-'to the list of operations for 'int'.For
intoperations, you'll need to decide what to do about numeric overflows and divide-by-zero conditions. Probably OK to fall back onint.erictraut commentedon Jan 14, 2022
Out of curiosity, are you planning to support unions?
erictraut commentedon Jan 14, 2022
One challenge to consider... If you're evaluating types in a loop, you need to be careful about literal math.
erictraut commentedon Jan 14, 2022
I think this is a good feature idea, so I implemented it in pyright. If you haven't already completed your PR for mypy, perhaps I can save you some time by sharing the unit tests I wrote.
AlexWaygood commentedon Jan 14, 2022
Could
*potentially be a supported operation for strings?'foo' * 2seems pretty similar to'foo' + 'foo'.GBeauregard commentedon Jan 14, 2022
You of course need to trust the code you run your LSP on, but what is the handling for bad input?
Intentionally trying to OOM:
I'm not sure how possible it is to accidentally create such a situation though.
sobolevn commentedon Jan 14, 2022
Thanks! I haven't touched unary operations right now. But, they are on the list!
Yes, this is a plan. In case of any problems / errors we escape from
Literalspecial path to ordinarycheck_opThank you, @erictraut!
Yes, looks like a good unit-test.
They are not supported right now. Probably in the future 🙂
Yes, I also tought about that. But, right now the logic is that type never changes for both operands.
str * intproducesstrwhich is the corner case. I will try it!sobolevn commentedon Jan 14, 2022
TODO:
UnaryOpOperatorAssignmentStmt?int: like^,|,**, etc'a' * 2Unionliterals1 // 0error message, we can totally catch that case and warn our usersa: Final = 'a'; reveal_type(f'{a}b')should revealLiteral['ab']?, probably this can be a separate featureGBeauregard commentedon Jan 14, 2022
Strings explode a bit more easily in similar fashion if they're added a few times.
Intended to OOM:
EDIT: This is far too much unneeded work, but I was thinking you can probably handle these cases without bailing to
intorstrif you avoid eager evaluation and instead put the corresponding problem into a SMT solver when you need to type check against a literal. 😄sobolevn commentedon Jan 14, 2022
@GBeauregard we don't support union type at the moment, but this is going to be a great corner-case in the future! 🎉
erictraut commentedon Jan 14, 2022
@GBeauregard, that test case is evil! I love it. :)