Skip to content

Conversation

@driazati
Copy link
Contributor

@driazati driazati commented Jun 7, 2019

This PR adds support for in checks like key in my_dict

For now it leaves lists as a follow up due to the changes around IValue lists and it needing an IValue equality op.

For objects it uses the magic method __contains__(self, key)

Differential Revision: D15811203

This PR adds support for `in` checks like `2 in [1, 2, 3]` or `key in
my_dict`

For objects it uses the magic method `__contains__(self, key)`
@pytorchbot pytorchbot added oncall: jit Add this issue/PR to JIT oncall triage queue module: internals Related to internal abstractions in c10 and ATen labels Jun 7, 2019
@driazati driazati requested review from eellison and suo June 13, 2019 20:23
if (kind == TK_FOR) {
in_for = true;
auto target = parseExp();
in_for = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work for something like: for i in range(d["a"] if "a" in d else 2):

@driazati driazati requested a review from zdevito June 13, 2019 22:29
Copy link
Contributor

@zdevito zdevito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is one bug in the implementation and the parser needs to be updated to distinguish variable declarations in for loops from expressions.

@pytorchbot pytorchbot added the module: pybind Related to our Python bindings / interactions with other Python libraries label Jun 14, 2019
@driazati driazati requested review from eellison and zdevito June 14, 2019 21:40
Copy link
Contributor

@zdevito zdevito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Just one minor suggestion.

auto named_values = getNamedValues(inputs, /*maybe_unpack=*/false);
auto class_arg_position = 0;

if (tree->kind() == TK_IN) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class_arg_position is weird here. This would be the appropriate time to flip the order and call the method. Previously it was invalid because the subexpressions were not yet emitted. But now they are (getNamedValues does it) and it would fit better in the API if it just called std::swap on named_values.


if (kind == TK_FOR) {
auto target = parseExp();
auto target = parseIdent();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why targets will be a Ident instead of Expr? things like a, (b, c) = 1, (2, 3) will not work. Why do you need to change it in the first place?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't support that currently so this isn't a regression at least (we just expect everything to be an Ident somewhere later in the compilation process. Expr includes in statements now to keep it from parsing the in on a for-in loop we need to parse the loop exprlist as just identifiers.

@facebook-github-bot
Copy link
Contributor

@driazati merged this pull request in 5eb25c3.

zdevito pushed a commit to zdevito/ATen that referenced this pull request Jun 18, 2019
Summary:
This PR adds support for `in` checks like `key in my_dict`

For now it leaves lists as a follow up due to the changes around `IValue` lists and it needing an `IValue` equality op.

For objects it uses the magic method `__contains__(self, key)`
](https://our.intern.facebook.com/intern/diff/15811203/)
Pull Request resolved: pytorch/pytorch#21527

Pulled By: driazati

Differential Revision: D15811203

fbshipit-source-id: 95745060394f8a9450efaaf8ab09d9af83bea01e
@facebook-github-bot facebook-github-bot deleted the driazati/forin branch July 13, 2020 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: internals Related to internal abstractions in c10 and ATen module: pybind Related to our Python bindings / interactions with other Python libraries oncall: jit Add this issue/PR to JIT oncall triage queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants