Skip to content

Detect variables that are used before they're defined - #14163

Merged
ilinum merged 3 commits into
python:masterfrom
ilinum:partial/use-before-def
Nov 22, 2022
Merged

ilinum merged 3 commits into
python:masterfrom
ilinum:partial/use-before-def

Conversation

@ilinum

@ilinum ilinum commented Nov 22, 2022

Copy link
Copy Markdown
Collaborator

This implements a check when a variable is defined before use. Something like:

def foo() -> None:
    x = y
    y: int = 1

I've combined this check with the partially defined check but added a separate error code. It's probably worth cleaning it up and making the separation between the two checks a bit more clear. I can do this in a follow-up PR.

Fixes #14124

@ilinum
ilinum requested a review from ilevkivskyi November 22, 2022 05:28
@github-actions

This comment has been minimized.

@ilevkivskyi ilevkivskyi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! Looks good, but please address two comments before landing this.

Comment thread mypy/errorcodes.py
"use-before-def",
"Warn about variables that are used before they are defined",
"General",
default_enabled=False,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should make this one enabled by default.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think that we need to understand the performance of the definedness analysis before enabling it by default. Also the implementation is still somewhat incomplete, and it would be good to fix known issues first.

In particular, the current implementation of try statements (#14114, currently not merged) seems to be exponential which might become expensive. Perhaps there is a more efficient way to do the same thing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We all agreed that always-undefined should be enabled by default in 1.0. Then there is no point in postponing this now, the sooner we enable this, the sooner we get some feedback, and will be able to fix possible problems. We will at least get feedback from mypy_primer.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A follow-up PR can be immediately created after this has been merged that turns this on by default to get feedback from mypy_primer. We don't need to wait until everything is complete (but we may want to wait for some additional changes before merging the PR).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OK, let's do it this way then.

pass
else:
y = z # E: Name "z" is used before definition
x = z # No error here -- we shouldn't report the error twice.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we should actually report both cases. Imagine you have

def some_long_func() -> None:
    x = z
    # 50 lines later
    y = z

In this case someone will need to fix first error, then run mypy again to get the second one and so on.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think it may be a bit cleaner to just report one error but let's report all and then see what real-world issues we see with it.

# Conflicts:
#	test-data/unit/check-partially-defined.test
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@ilinum
ilinum merged commit f656efc into python:master Nov 22, 2022
@ilinum
ilinum deleted the partial/use-before-def branch November 22, 2022 21:57
JukkaL pushed a commit that referenced this pull request Jan 20, 2023
This enables the error code added in #14163.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[partially defined] Doesn't catch usage within an if statement

3 participants