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

Handle dotted alias imports to check for implicit imports #4685

Merged
merged 4 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_type_checking/strict.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,41 @@ def f():

def test(value: A):
return pkg.B()


def f():
# In un-strict mode, this shouldn't rase an error, since `pkg.bar` is used at runtime.
import pkg
import pkg.bar as B

def test(value: pkg.A):
return B()


def f():
# In un-strict mode, this shouldn't rase an error, since `pkg.foo.bar` is used at runtime.
import pkg.foo as F
import pkg.foo.bar as B

def test(value: F.Foo):
return B()


def f():
# In un-strict mode, this shouldn't rase an error, since `pkg.foo.bar` is used at runtime.
import pkg
import pkg.foo.bar as B

def test(value: pkg.A):
return B()


def f():
# In un-strict mode, this _should_ rase an error, since `pkgfoo.bar` is used at runtime.
# Note that `pkg` is a prefix of `pkgfoo` which are both different modules. This is
# testing the implementation.
import pkg
import pkgfoo.bar as B

def test(value: pkg.A):
return B()
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,20 @@ fn is_implicit_import(this: &Binding, that: &Binding) -> bool {
}
BindingKind::Importation(Importation {
full_name: that_name,
..
})
| BindingKind::SubmoduleImportation(SubmoduleImportation {
name: that_alias,
}) => {
if that_name == that_alias {
// Ex) `pkg` vs. `pkg`
this_name == that_name
} else {
// Submodule importation with an alias: `import pkg.A as B`
this_name
.split('.')
.zip(that_name.split('.'))
.all(|(this_part, that_part)| this_part == that_part)
dhruvmanila marked this conversation as resolved.
Show resolved Hide resolved
}
}
BindingKind::SubmoduleImportation(SubmoduleImportation {
name: that_name, ..
}) => {
// Ex) `pkg.A` vs. `pkg.B`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,40 @@ strict.py:54:25: TCH002 Move third-party import `pkg.bar.A` into a type-checking
58 | def test(value: A):
|

strict.py:62:12: TCH002 Move third-party import `pkg` into a type-checking block
|
62 | def f():
63 | # In un-strict mode, this shouldn't rase an error, since `pkg.bar` is used at runtime.
64 | import pkg
| ^^^ TCH002
65 | import pkg.bar as B
|

strict.py:71:12: TCH002 Move third-party import `pkg.foo` into a type-checking block
|
71 | def f():
72 | # In un-strict mode, this shouldn't rase an error, since `pkg.foo.bar` is used at runtime.
73 | import pkg.foo as F
| ^^^^^^^^^^^^ TCH002
74 | import pkg.foo.bar as B
|

strict.py:80:12: TCH002 Move third-party import `pkg` into a type-checking block
|
80 | def f():
81 | # In un-strict mode, this shouldn't rase an error, since `pkg.foo.bar` is used at runtime.
82 | import pkg
| ^^^ TCH002
83 | import pkg.foo.bar as B
|

strict.py:91:12: TCH002 Move third-party import `pkg` into a type-checking block
|
91 | # Note that `pkg` is a prefix of `pkgfoo` which are both different modules. This is
92 | # testing the implementation.
93 | import pkg
| ^^^ TCH002
94 | import pkgfoo.bar as B
|


Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ strict.py:54:25: TCH002 Move third-party import `pkg.bar.A` into a type-checking
58 | def test(value: A):
|

strict.py:91:12: TCH002 Move third-party import `pkg` into a type-checking block
|
91 | # Note that `pkg` is a prefix of `pkgfoo` which are both different modules. This is
92 | # testing the implementation.
93 | import pkg
| ^^^ TCH002
94 | import pkgfoo.bar as B
|