45 Commits
Author SHA1 Message Date
Andrey Vokin 7510fc1706 PY-78371 PyCharm does not mark imports in try/except blocks as unused
Update test data.
1. Unresolved imports are not removed with optimize imports
2. Unresolved imports are not marked as unused

GitOrigin-RevId: 2c32c368ae0907fb75ea76fc9749e903c160667c
2025-01-16 21:34:27 +00:00
Aleksei Kniazev 0d5f27c47d imports are inserted after module-level dunder names to comply with PEP-8 (PY-23475)
GitOrigin-RevId: 9cad837e708f3c9e52abea59d9e239371cb515bc
2019-09-27 16:33:17 +00:00
Mikhail Golubev 0eb24a8b98 PY-20100 Refactor PyImportOptimizer according to comments in IDEA-CR-37663
First of all, removed duplication and slightly simplified convoluted
transformImportStatements() method.

Also, extended existing test to include handling of comments when splitting
"from" imports.
2018-10-10 14:54:01 +03:00
Mikhail Golubev e7a76b7a28 PY-20100 Add an option to always split "from" imports in Optimize Imports 2018-10-03 17:13:23 +03:00
Mikhail Golubev 841fcbc914 PY-20159 Add an option to make optimize imports case-insensitive
Both packages/module names and names of individual imported symbols in
"from" imports are sorted case-insensitively if it's enabled.
To keep things simple I decided not to replicate the exact scheme used
by isort to order names in "from" imports where they have different priority
depending on whether they are ALL_CAPS or CamelCased (check the issue for
related discussion).
2018-10-01 12:07:13 +03:00
Mikhail Golubev dcb69ad408 PY-25567 Keep parentheses around re-ordered from imports in optimize imports
It doesn't affect new imports received as a result of joining several
existing ones sharing the same source. For them user must explicitly
set preferred type of formatting in code style settings.
2017-10-23 18:58:30 +03:00
Mikhail Golubev 041fe2b1be PY-23578 Optimize Imports preserves a blank line before the first import import
It happened due to the bug in the platform and only when the number of
blank lines after the import statement is greater than that before it.

As a temporary workaround before the relevant fix in the platform is
accepted I delete old imports as text through the underlying document.
2017-04-10 14:45:09 +03:00
Mikhail Golubev d73b5d2580 PY-22656 Don't use location of .pyi stubs to determine import groups in Optimize Imports
The problem was partially fixed for the stdlib modules with .pyi stubs
from typeshed in e990277415
2017-04-04 14:24:46 +03:00
Mikhail Golubev 6de5015a72 PY-23125 Preserve blank lines before the first comment in optimize imports
I tweaked Python import optimizer so that it inserts new imports
and their comments after the last of original imports, not before the
first of them (and then deletes the old ones). The reason is that it
turned out to be practically impossible to use PsiComment as the anchor
for PsiElement#addRangeBefore(), because of the hardcoded behavior of
CodeEditUtil.addChildren() which skips preceding whitespaces in this
scenario.

Otherwise, if the blank line before "# noinspection" comment preceding
the first imports statement gets removed, next time this comment will be
left in place, considered part of the top-level module comment
e.g. license.
2017-03-17 14:44:29 +03:00
Mikhail Golubev 6ee108b175 PY-23125 Optimize imports stacks unbound comments at the end of the import block
Optimize imports now differentiate between "bound" and "unbound" comments
interleaving import statements. The former immediately precede
the import after them without any blank lines between them, while all
the rest are considered the latter, "unbound", comments which are
grouped and inserted after the whole import block. It allows to handle
the comments before the first import more accurately, moving comments
like "# noinspection" as expected, yet leaving licenses, shebangs and
encoding declarations in place if they are separated with a blank line.

Additionally, this is almost identical to the way "isort" utility
handles line comments.
2017-03-17 14:44:29 +03:00
Mikhail Golubev 87d31be6c5 PY-23104 Inline multiline "name as alias" pairs in optimize imports 2017-03-17 14:44:29 +03:00
Mikhail Golubev b882f53851 PY-23035 PY-23086 Handle comments in "from" imports better in optimize imports
by joining their text via "; " the same way isort deals with them
2017-03-17 14:44:29 +03:00
Mikhail Golubev 44f1f5608d PY-19837 Keep comments before the first import when optimizing imports
They usually serve some specific purpose, e.g. general information
about the module, licence, shebang comment, etc.
Overall, it seems safer to keep them intact.

As suggested in IDEA-CR-19238
2017-03-13 23:48:40 +03:00
Mikhail Golubev 3d8d7cda5d PY-19837 Preserve trailing comments in optimize imports
When there are a few such comments after "from" imports that are
going to be combined into one, the first of the comments is inserted
on the same line after the statement, and the remaining ones appear
below as extra line comments.
2017-03-09 16:49:34 +03:00
Mikhail Golubev 9da18a8567 PY-19837 Keep line comments in optimize imports
Basically, collect line comments before each import statement and insert
them back when generating the new import block.

A few corner cases:
If a normal import statement is split into several new ones, all its
comments appear before the first of these partial statements.
On the other hand, if several "from" imports are combined into one,
their comments are joined together in the same order as the names listed
in these statements (they may get additionaly sorted).
Shebang comments are ignored.
2017-03-09 16:49:34 +03:00
Mikhail Golubev aaef12ce91 PY-22355 Optimize imports honors new code style settings for "from" imports
Namely, it automatically surrounds them with parentheses and inserts
a trailing comma after the last imported name if necessary, e.g. when
several "from" imports with the same source were combined into a
single long statement that needs to be wrapped.
2017-03-06 17:15:58 +03:00
Mikhail Golubev 514961db75 PY-18972 Add tests on f-strings in unresolved references and optimize imports 2016-10-05 16:11:37 +03:00
Mikhail Golubev 33012c4c70 PY-19836 When sorting imports by names use their aliases as tiebreaker
It's necessary for the rare case when the same definition, module or
package is imported multiple times with different aliases or both
with and without alias.
2016-06-15 20:46:01 +03:00
Mikhail Golubev e5c79dde87 PY-18792 Allow to order imports within the same group first by name, then by type
This way several imports for the same module or package are placed
together regardless of their type. For instance, "import pkg1",
"import pkg2", "from pkg1 import a" are ordered as "import pkg1",
"from pkg1 import a", "import pkg2", i.e. imports of the same kind are
not necessarily put together since names of qualifier and imported
symbols have greater priority.

This is similar to the style "google" of flake8-import-order package and
flag "--force-sort-within-sections" of isort.
2016-06-15 19:34:14 +03:00
Mikhail Golubev 9df1dacb23 PyFile#getImportBlock works better when there are comments in-between or file ends with import 2016-06-15 19:34:13 +03:00
Mikhail Golubev 5e045e5c12 PY-19674 Treat all relative (with dots) "from" imports as application local imports 2016-06-15 19:34:13 +03:00
Mikhail Golubev 2034207ca7 PY-18792 Move messages into PyBundle 2016-06-15 19:34:12 +03:00
Mikhail Golubev 00bb34d627 PY-14176 "Join from imports from same source" handles relative and star imports 2016-06-15 19:34:12 +03:00
Mikhail Golubev a9ea4677bc PY-19292 PY-14176 Optimize Imports doesn't re-create unaffected import statements
For instance, "from" import statements where imported names are already
ordered. Otherwise even when all imports are well formed, the whole
import block will be inserted anew and reformatted.
2016-06-15 19:34:12 +03:00
Mikhail Golubev 84381c7273 PY-18792 Add several new options for Optimize Imports in Python
Namely allow to:
* disable alphabetical ordering of imports
* order individual imported names inside "from" import (PY-19292)
* combine multiple "from" imports with the same source (PY-14176)
2016-06-15 19:34:11 +03:00
Mikhail Golubev d36f47ddd7 PY-18970 Check that imports in "project" group don't belong to any library root
Consider such imports as imports from the standard library or third party
libraries instead.
2016-05-04 14:12:10 +03:00
Mikhail Golubev 65c20d7064 PY-18521 Consider references inside function type comments to mark corresponding imports as used
Additionally PyImportOptimizer visits PSI comments collecting
information about unused imports, otherwise it doesn't take into
account references inside any kind of type comments.
2016-02-16 15:16:56 +03:00
Andrey Vlasovskikh 359b13a62f Resolve PyReferenceImpl refs to all outer definitions in the current file
This applies to all outer definitions except imports where we resolve
only to the latest imports as we used to do. This is required for
detecting and optimizing unused imports and adding imports during
refactorings.

The implementation of ResolveProcessor has been rewritten as
PyResolveProcessor.
2016-01-20 13:14:26 +03:00
Mikhail Golubev dce7b23ff3 PY-12018 Relative imports are always placed in the end of a group
Also use lexicographical ordering among all names in an import
statement for better tie breaking.
2015-09-10 19:02:53 +03:00
Mikhail Golubev 2b0a11e466 PY-12018 Sort import by their types and names similar to the behavior of isort
Normal imports go first, then "from" imports.
2015-09-10 19:02:53 +03:00
Mikhail Golubev a49ba06f5c PY-12018 Optimize imports sorts and adds empty line after __future__ imports
For consistency all unresolved imports are treated as imports from
project. Previously (controversially) some of them were recognized as
BUILTIN imports and some as PROJECT imports.
2015-09-10 19:02:52 +03:00
Mikhail Golubev aed3a6e47d PY-16351 Ignore not inline comments to detect proper spacing between declarations 2015-07-06 12:48:24 +03:00
Mikhail Golubev 4237819376 PY-14765 Two "from imports" with the same source module are ordered by the first imported name
Additionally star imports always come first
2015-06-22 17:33:57 +03:00
Mikhail Golubev 3f332ff775 PY-12018 First attempt to sort imports alphabetically in ImportOptimizer 2015-06-22 17:33:56 +03:00
Mikhail Golubev 32b76de893 PY-11552 Keep maximum 1 empty line between declarations and in code by default
Fix affected test data where reformat is invoked during refactoring.
Also use entire sample provided by user in the corresponding test case
for the issue.
2015-04-27 20:34:47 +03:00
Ilya.Kazakevich 09916b56ca PY-6955 Unused import not greyed out if also failing 2014-01-21 04:18:46 +04:00
Dmitry Jemerov dd5841d56d optimize imports inserts blank lines between groups also when we don't have any missorted imports (PY-8355) 2013-11-04 18:55:32 +01:00
Dmitry Jemerov 4216441340 import sorter skips from __future__ imports (PY-10022) 2013-10-31 21:00:16 +01:00
Dmitry Jemerov 6bb86279d1 optimize imports sorts them according to PEP-8 (PY-2367) 2012-10-02 20:04:45 +02:00
Dmitry Jemerov a80df9b3a9 optimize imports doesn't remove imports for which inspection is suppressed (PY-5228) 2011-12-09 20:32:26 +01:00
Dmitry Jemerov a6137e77f9 Optimize Imports shouldn't delete unused imports (PY-2201) 2010-11-01 22:42:11 +03:00
Dmitry Jemerov 9f0f1b8f7d from __future__ imports aren't unused 2010-07-01 21:53:46 +04:00
Dmitry Jemerov e118ec0cdb testdata corrected 2010-05-14 11:53:29 +04:00
Dmitry Jemerov 1df360cfdf imports inside try/except statements shouldn't be optimized away 2010-04-30 21:36:31 +04:00
Dmitry Jemerov 847800c572 initial implementation of 'optimize imports' for Python 2010-04-30 21:36:29 +04:00