Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incompatible_assignment_identifiers_have_local_scope: Variable assignment inside function is not treated as local #8956

Closed
Quarz0 opened this issue Jul 23, 2019 · 3 comments

Comments

@Quarz0
Copy link
Contributor

@Quarz0 Quarz0 commented Jul 23, 2019

Description of the problem / feature request:

Augmented assignment statements do not make the LHS identifier local, thus variables declared outside a function are allowed to be assigned inside a function without being treated as local.

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

a_list = []
def f4():
    a_list += [1]
f4()
print(a_list) # [1]

Running the same code using the Go implementation or Python:

a_list = []
def f4():
    a_list += [1]
f4() # local variable a_list referenced before assignment

Suggested fix: use list.append or list.extend instead of +=.

a_list = []
def f4():
    a_list.append(1)
f4() # valid

Any other information, logs, or outputs that you want to share?

https://stackoverflow.com/questions/6797164/why-doesnt-pythons-plus-equals-operator-modify-variables-from-inner-functio/6797210#6797210

Quarz0 added a commit to Quarz0/bazel that referenced this issue Jul 23, 2019
…=false

When this flag is enabled, LHS identifiers in assignment statements
become local to the block contatining them.

RELNOTES: Flag `--incompatible_assignment_identifiers_have_local_scope`
is added. See bazelbuild#8956
@bazel-io bazel-io closed this in bf80bb0 Jul 25, 2019
@laurentlb laurentlb reopened this Jul 28, 2019
@katre katre added migration-0.29 and removed migration-ready labels Aug 2, 2019
@dslomov

This comment has been minimized.

Copy link
Contributor

@dslomov dslomov commented Aug 5, 2019

Is this realistic to flip in 1.0?
Do we have any migration tooling?

@laurentlb

This comment has been minimized.

Copy link
Member

@laurentlb laurentlb commented Aug 5, 2019

I expect the number of breakages to be very low. I've run it on some projects inside Google (27k packages loaded, 690k targets configured), I couldn't detect any breakage.

In case of breakage, the fix should be simple to do manually. I'll update the first message.

@laurentlb laurentlb changed the title Variable assignment inside function is not treated as local incompatible_assignment_identifiers_have_local_scope: Variable assignment inside function is not treated as local Aug 5, 2019
@laurentlb

This comment has been minimized.

Copy link
Member

@laurentlb laurentlb commented Aug 5, 2019

Apparently, there's no breakage in Baze downstream projects either: https://buildkite.com/bazel/bazelisk-plus-incompatible-flags/builds/196

@bazel-io bazel-io closed this in 80a4670 Aug 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.