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

Emit SyntaxWarning for code that risks UnboundLocalError #60633

Closed
ncoghlan opened this issue Nov 7, 2012 · 2 comments
Closed

Emit SyntaxWarning for code that risks UnboundLocalError #60633

ncoghlan opened this issue Nov 7, 2012 · 2 comments
Labels
type-feature A feature request or enhancement

Comments

@ncoghlan
Copy link
Contributor

ncoghlan commented Nov 7, 2012

BPO 16429
Nosy @birkenfeld, @ncoghlan, @asvetlov, @durban

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2017-03-07.19:24:04.815>
created_at = <Date 2012-11-07.14:59:26.405>
labels = ['type-feature']
title = 'Emit SyntaxWarning for code that risks UnboundLocalError'
updated_at = <Date 2017-03-07.19:24:04.814>
user = 'https://github.com/ncoghlan'

bugs.python.org fields:

activity = <Date 2017-03-07.19:24:04.814>
actor = 'serhiy.storchaka'
assignee = 'none'
closed = True
closed_date = <Date 2017-03-07.19:24:04.815>
closer = 'serhiy.storchaka'
components = []
creation = <Date 2012-11-07.14:59:26.405>
creator = 'ncoghlan'
dependencies = []
files = []
hgrepos = []
issue_num = 16429
keywords = []
message_count = 2.0
messages = ['175107', '228683']
nosy_count = 4.0
nosy_names = ['georg.brandl', 'ncoghlan', 'asvetlov', 'daniel.urban']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue16429'
versions = ['Python 3.5']

@ncoghlan
Copy link
Contributor Author

ncoghlan commented Nov 7, 2012

The compiler is actually in a fairly good position to tell when code is at risk of triggering UnboundLocalError at runtime: specifically, in the section of the code that checks for duplicated parameter names [1]

Now, we can't emit SyntaxError here for backwards compatibility reasons (if you have an early reference that is never executed, your code is dodgy but will still run OK).

However, we should be able to emit a Syntax *Warning* when we detect an existing symbol at function scope having DEF_LOCAL applied for the first time *after* it has already been referenced in a way which doesn't create a local variable.

Something like:

SyntaxWarning: Local variable NAME bound after earlier reference (risks UnboundLocalError when function is called)

[1] http://hg.python.org/cpython/file/default/Python/symtable.c#l1002

@BreamoreBoy BreamoreBoy mannequin added the type-feature A feature request or enhancement label Aug 3, 2014
@birkenfeld
Copy link
Member

I seem to recall that this was rejected into the realm of linters in a python-dev discussion.

If there is a chance of false positives, having Python emit the warning would be annoying because there is no convenient way of telling it to shut up about it. At least people can choose not to run linters, or configure them to their taste.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants