Repository navigation
Report possibly uninitialized attributes with a dedicated strict check #4326
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
Open
asukaminato0721
wants to merge
9
commits into
facebook:main
Choose a base branch
from
asukaminato0721:4318
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,657
−61
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4c3d023
fix
asukaminato0721 4c2a8d1
keep init function's priority, and merge helper
asukaminato0721 aedd4c8
keep is fn def in flow info
asukaminato0721 a01848b
fix `and` `or` merge
asukaminato0721 d38b07d
new error msg
asukaminato0721 a4058d3
skip pyi check
asukaminato0721 1d506ff
add bench, fix some perf
asukaminato0721 6440b7d
Bound attribute initialization proofs and isolate flow changes
asukaminato0721 cc54c04
.
asukaminato0721 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| //! Measure edits to a file containing a constructor with many attributes and joins. | ||
| //! Server initialization and the first check are outside the measured region. | ||
|
|
||
| use std::fmt::Write as _; | ||
| use std::fs; | ||
|
|
||
| use criterion::Criterion; | ||
| use criterion::criterion_group; | ||
| use criterion::criterion_main; | ||
| use lsp_types::DidChangeTextDocumentNotification; | ||
| use lsp_types::Uri; | ||
| use pyrefly::commands::lsp::IndexingMode; | ||
| use pyrefly::commands::lsp::LspArgs; | ||
| use pyrefly_lsp_test::object_model::InitializeSettings; | ||
| use pyrefly_lsp_test::object_model::LspInteraction; | ||
| use pyrefly_lsp_test::object_model::LspInteractionArgs; | ||
| use pyrefly_util::telemetry::NoTelemetry; | ||
| use pyrefly_util::thread_pool::TEST_THREAD_COUNT; | ||
| use serde_json::json; | ||
| use tempfile::tempdir; | ||
|
|
||
| fn attribute_initialization_edit(c: &mut Criterion) { | ||
| let dir = tempdir().unwrap(); | ||
| fs::write( | ||
| dir.path().join("pyrefly.toml"), | ||
| "[errors]\npossibly-uninitialized-attribute = 'error'\n", | ||
| ) | ||
| .unwrap(); | ||
| let path = dir.path().join("constructor.py"); | ||
| let mut source = | ||
| String::from("class C:\n def __init__(self, flag: bool, values: list[int]) -> None:\n"); | ||
| for i in 0..64 { | ||
| let _ = writeln!(source, " self.attribute_{i}: int = 0"); | ||
| } | ||
| for i in 0..64 { | ||
| let _ = writeln!( | ||
| source, | ||
| " for value in values:\n if flag:\n self.attribute_{i} = value" | ||
| ); | ||
| } | ||
| let with_error = format!("{source}\nresult: int = 'error'\n"); | ||
| fs::write(&path, &source).unwrap(); | ||
| let mut interaction = LspInteraction::new_with_args(LspInteractionArgs { | ||
| args: LspArgs { | ||
| indexing_mode: IndexingMode::None, | ||
| workspace_indexing_limit: 50, | ||
| build_system_blocking: false, | ||
| }, | ||
| telemetry: Box::new(NoTelemetry), | ||
| thread_count: TEST_THREAD_COUNT, | ||
| thrift_remapper: None, | ||
| }); | ||
| interaction.set_root(dir.path().to_path_buf()); | ||
| interaction | ||
| .initialize(InitializeSettings { | ||
| configuration: Some(Some(json!([ | ||
| {"pyrefly": {"displayTypeErrors": "force-on"}} | ||
| ]))), | ||
| ..Default::default() | ||
| }) | ||
| .unwrap(); | ||
| interaction.client.did_open("constructor.py"); | ||
| interaction | ||
| .client | ||
| .expect_publish_diagnostics_eventual_error_count(path.clone(), 0) | ||
| .unwrap(); | ||
|
|
||
| let mut version = 1; | ||
| c.bench_function("attribute_initialization/edit_to_diagnostics", |b| { | ||
| b.iter(|| { | ||
| version += 1; | ||
| let has_error = version % 2 == 0; | ||
| let text = if has_error { &with_error } else { &source }; | ||
| interaction | ||
| .client | ||
| .send_notification::<DidChangeTextDocumentNotification>(json!({ | ||
| "textDocument": { | ||
| "uri": Uri::from_file_path(&path).unwrap(), | ||
| "version": version, | ||
| }, | ||
| "contentChanges": [{"text": text}], | ||
| })); | ||
| // Alternating the error count ensures that each edit completes. | ||
| interaction | ||
| .client | ||
| .expect_publish_diagnostics_eventual_error_count( | ||
| path.clone(), | ||
| usize::from(has_error), | ||
| ) | ||
| .unwrap(); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| criterion_group!(benches, attribute_initialization_edit); | ||
| criterion_main!(benches); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We never check to see if an attribute is defined on a base class, so something like this would fail:
The same thing happens with
super().__init__()We should be able to fix this by skipping the error when a base other than
objectdefines the nameCan we also add a test for this one