Commit Graph

78 Commits

Author SHA1 Message Date
Petr
ff19de5654 PY-81646 False positive: Unresolved references after any slice in Python for the rest of the line
Fixed AST for PySubscriptionExpression containing a slice.
Before PySubscriptionExpression.getIndexExpression() returned only the first slice expression (if any).

Also fixed PySliceItem's subtree, so now its children are correctly mapped to lowerBound, upperBound and stride properties.

GitOrigin-RevId: 840af92957d0431679cf7a477866dea76320ca80
2025-06-13 18:59:31 +00:00
Ilya.Kazakevich
80f248d842 Python: Move helpers locator out of PSI module
GitOrigin-RevId: 55fb4b41c56fdd41b6d9523cd7a148c002ffb8be
2025-06-07 21:59:42 +00:00
evgeny.bovykin
aaa808c3b4 Revert "PY-79480 Resolve attribute reference to ancestor attributes in constructors if they contain type annotations"
Fixes PY-79997 performance regression

GitOrigin-RevId: 9bccec0543fcdf7970311c187bd9fd9b1357c058
2025-03-31 07:44:23 +00:00
chbndrhnns
91bddd1e62 PY-54167 Add import statement for class when using completion in
__all__

GitOrigin-RevId: 4e54ad17a5965e80557e822ae6a9d323da428f9f
2025-03-19 23:50:11 +00:00
evgeny.bovykin
e3c3cabc3f PY-79480 Resolve attribute reference to ancestor attributes in constructors if they contain type annotations
GitOrigin-RevId: 73d7695805134c3b7b7f557badc794186cda9a55
2025-03-06 12:29:59 +00:00
Aleksandr.Govenko
0c54074d30 PY-26947 Class variable mistaken as module global variable
Prevent resolving class-level target expressions in global scope.


Merge-request: IJ-MR-155098
Merged-by: Aleksandr Govenko <aleksandr.govenko@jetbrains.com>

GitOrigin-RevId: 2452801387ee2480e61b2f5119e7f3f2704cb4ef
2025-02-20 19:19:41 +00:00
Daniil Kalinin
97ba49d547 PY-26877 Support adding trailing commas to multiline collections, argument and parameter lists
Merge-request: IJ-MR-153633
Merged-by: Daniil Kalinin <Daniil.Kalinin@jetbrains.com>

GitOrigin-RevId: b304977e30eb4b378d858fcc5300846dc5965445
2025-02-14 08:46:05 +00:00
Daniil Kalinin
fddb487bf8 PY-31689 Do not wrap tuples and list literals inside subscription expressions
GitOrigin-RevId: 9878660c10bab6bda36aa5f2bcd4c96f416dddb8
2025-02-13 10:45:38 +00:00
Daniil Kalinin
5b0b94a8dd [python] Formatter: Make sure that comments in collections are not placed on the new line on wrap
* Extract getting wrapping for collections and arg/param lists into a separate method
* Add a test

GitOrigin-RevId: 7ac252e5ab414329951adc61ea0dd29beeb27c04
2025-02-13 10:45:38 +00:00
Daniil Kalinin
2d7ad0e1fb PY-31689 Support wrapping options for parenthesized tuples
GitOrigin-RevId: 2b14686ea8c691627c9a8155f75a7c6727a48bf3
2025-02-13 10:45:38 +00:00
Daniil Kalinin
b1b0a08fbc PY-31689 Support wrapping options for set literals
GitOrigin-RevId: 6f76a929da4bde150339ab924f4e84f91d70d8a3
2025-02-13 10:45:38 +00:00
Daniil Kalinin
f9a1e44638 PY-31689 Support wrapping options for list literals
GitOrigin-RevId: be976ddc224983174f5566e53985e29fd49051f4
2025-02-13 10:45:38 +00:00
Nikita.Ashihmin
6187f47433 PY-78425 Jupyter: Fix tests for Single file
GitOrigin-RevId: 1857fb5c6b675e2bb9ef58836cb324622828bd8e
2025-01-14 14:40:10 +00:00
Mikhail Golubev
9b67a1fea4 PY-74114 Dynamically enable/disable bundled and Typeshed .pyi stubs for third-party packages
Stubs for a third-party package are enabled only if:
 - this package is actually installed on the project interpreter
 - there are neither "<pkg-name>-stub" package, nor "types-<pkg-name>" installed
 on the project interpreter (types-<pkg-name> are Typeshed stubs re-published on PyPI).

The latter rule allows users to install more recent versions of stub packages from PyPI.

A few tests that relied on presense of .pyi stubs for some packages were updated to
explicitly add those to SDK roots.

GitOrigin-RevId: 8460c0d3695405f9e5cf0427b05a1f2744055277
2025-01-06 17:58:30 +00:00
Mikhail Golubev
ccd7847c0a PY-74116 Move other tests on completion of importable name from typing to Py3CompletionTest
And make their setup similar to
testParenthesesAreNotInsertedAfterNamesDefinedAsFunctionsInTypingPy.

typing.py in the test directory has higher priority than typing.pyi in Typeshed,
as a user .py file (see PyResolveImportUtil.Priority), so
PyResolveImportUtil.resolveQualifiedName can't find typing.pyi, and it's not used
in the tests.

GitOrigin-RevId: 3dd51af04f5e5e16e7167dea32b248bca240b18e
2025-01-06 10:31:06 +00:00
Mikhail Golubev
820da7c49c PY-77940 Allow resolving underscored names in .pyi stubs
This reverts the changes made in eb733982acf0992fbae822d0feb582fb07ad010f
for PY-40233.

The original problem was PyShadowingBuiltinsInspection erroneously reporting
redefining private names declared in builtins.pyi, such as _T, however,
resolving underscored names from all .pyi stubs was prohibited to fix that.

These are considered "stub-only objects" according to the typing spec
(https://typing.readthedocs.io/en/latest/guides/writing_stubs.html#stub-only-objects),
but it doesn't prohibit reusing them in other stubs and in .py files under
an `if typing.TYPE_CHECKING:` guard.
Besides, both Mypy and Pyright resolve such names imported from .pyi stubs without warnings,
so it doesn't make sense to enforce additional visibility restrictions from our side.

Also, I enabled completing such names in other .pyi stubs (but not in .py files).
Previously, in PY-38172 their completion was disabled everywhere.

GitOrigin-RevId: 8340d4dcaf732d18d481bbee824ffdddd2719d3d
2024-12-18 16:55:26 +00:00
Mikhail Golubev
cb009592c3 PY-77168 Allow flow-sensitive resolve from unmatched version checks to reachable outer blocks
Also, don't query LanguageLevel for each element of each instruction,
only once per scope traversal.

This doesn't fix the problem with unreachable definition *inside* blocks under
unmatched version checks, i.e.

if sys.version_info < (3, 8):
    Alias = int
    expr: Alias # unresolved

but it's a more difficult problem how to handle those consistently with the idea
of unreachable version checks under the *current* interpreter version, and hopefully
it occurs rarer than, say, unresolved top-level imports of common names from typing.

GitOrigin-RevId: 55fd4597c6d0860d290caba15fbf4d313e985a86
2024-11-19 11:53:33 +00:00
Irina Fediaeva
01e08f43e4 PY-52574: Update tests after removing Epytext docstring format
GitOrigin-RevId: d4a90a8da56ca889cf380aa5bc72ac82b0716abc
2024-10-29 21:03:06 +00:00
Mikhail Golubev
58d00407db PY-77060 Remove spaces after * and ** in new-style PEP 695 type parameters
GitOrigin-RevId: cd38f49d0f28894e4f33dbd2fa331eaf895fd70d
2024-10-29 13:37:39 +00:00
Petr
db52d4ec3d PY-34617 Take into account sys.version_info checks when analyzing Python files
Support version checks for import statements.

GitOrigin-RevId: df52f60574962e1bc222121aadc082683de0a869
2024-09-05 11:17:15 +00:00
Petr
79dc479c63 PY-34617 Take into account sys.version_info checks when analyzing Python files
Support and, or, <=, > operators in version checks.

GitOrigin-RevId: 5006e88b0f7935d0bf0841dfd5fad5c371e8ff12
2024-09-05 11:17:15 +00:00
Petr
93b9066edf PY-34617 Support version check
GitOrigin-RevId: 3318ff79cdcc5ba0ce5e4feb65abad5ad0f4acfa
2024-07-28 00:24:15 +00:00
Petr
5147d37e25 PY-34617 Code deduplication (PythonCommonCompletionTest)
GitOrigin-RevId: 20d48eb4e6d5620c182d388398dd80234b2eefe7
2024-07-28 00:24:15 +00:00
Mikhail Golubev
ebba681c85 PY-62208 Don't suggest names shorter than five characters unless it's an extended completion
Otherwise, we end up with dozens of unintentionally public names such as "s", "i", "k"
even in the standard library (e.g. `this.s` or `pickletools.i`).

Ideally, we should rely on .pyi stubs and the content of `__all__` to offer only explicitly
exposed API, but not every module has any of those two, and it's not clear how to match
.py files and the corresponding .pyi stubs fast enough for completion.

GitOrigin-RevId: 163c472654e60ae63ff893142b8ddb9accc56393
2024-07-02 14:08:48 +00:00
Mikhail Golubev
52850e21d8 PY-62208 Include importable names in basic completion results
Previously, such names were visible only on so-called "extended" completion,
activated when the hotkey for the basic completion was hit twice. The main reason
was that collecting such variants from indexes was a slow process, and we
didn't want to harm the responsiveness of completion for basic names.
Now it becomes possible thanks to a number of performance optimizations:

* Instead of using three separate indexes for classes, functions and variables,
we use one -- PyExportedModuleAttributeIndex. By definition, it includes only top-level
"importable" names, so we additionally save time by not filtering out irrelevant
entries. Also, it doesn't contain private definitions starting with an underscore.
It might bother some users, but given that the previous completion was used
extremely rarely, and the new one is going to be visible everywhere, it seems
that pruning unlikely entries as much as possible is a fare tradeoff. In the future,
we might enable them back on the "extended" completion if there is a demand.
Also, this index binds its keys to the project (`traceKeyHashToVirtualFileMapping`),
further eliminating useless index lookups.

* Thanks to the recent fixes in the platform (IJPL-265), it's now possible to
simultaneously iterate over all keys in an index and request values for a given key
without deadlocks, which is much faster than eagerly fetching all keys first.

* While scanning through all matching entries from indexes, we terminate
the lookup if the number of items exceeds the size of the lookup list.
We can further reduce this number by adjusting the "ide.completion.variant.limit"
registry value.

* Calculating expensive "canonical" import paths (e.g. "pkg.private.Name" is importable as
"pkg.Name") is offloaded to a background thread thanks to the `withExpensiveRenderer` API.
We still calculate these paths synchronously, though, for names whose raw qualified names
contain components starting with an underscore to decide whether these private names are
publicly re-exported and, hence, should be displayed.

The rest of the work has been put into reducing the number of entries on the list, e.g.

* The prefix under caret is now matched from the beginning of a name, e.g. `Bar<caret>`
matches `BarBaz`, but not `FooBar`.
* We don't suggest imported names clashing with those already available in scope.
* Some kinds of definitions are not suggested in specific contexts, e.g.
functions and variables are not suggested inside patterns and type hints.
* Nothing is suggested at the top-level of a class body, where dangling
reference expressions or calls are not normally expected.

Additionally, we don't suggest names from .pyi stubs at the moment, because
it pollutes the suggestion list with entries coming from the stubs for
third-party packages in Typeshed. We should probably enable them back once
we are able to properly disable Typeshed entries for not installed packages.

Some legacy forms of completion are left in the extended mode. In particular,
qualified names of classes are offered inside string literals only in this mode.
Also, module and package names are suggested only in the extended mode, because
top-level packages and modules are already suggested for the basic completion
by PyModuleNameCompletionContributor.

A few tests in PyClassNameCompletionTest were updated or removed entirely because
* we no longer suggest private names
* we no longer suggest names from private modules not re-exported in a public module
* we no longer suggest names clashing with those already available in scope
* prefix matching policy was changed to start at the beginning of an identifier

The whole feature can be disabled with the option "Suggest importable classes,
functions and variables in basic completion" in settings.

GitOrigin-RevId: 0787d42ce337b73b01a60f0bb7aa434fee43e659
2024-07-02 14:08:48 +00:00
Petr
feb3bdc4c2 PY-52502 Duplicate completion variants when local variable is used as map key
GitOrigin-RevId: 463225922b2f0201b5d1ed2749f4175a77fd543f
2024-05-14 01:09:52 +00:00
Petr
68c421f3d9 PY-42738 Incomplete handling of single and double quotes for dictionary keys in auto completion
GitOrigin-RevId: ed480e40c4de292d38efbe59656cf3c8eb234435
2024-04-25 23:00:28 +00:00
Petr
2409f9f494 [python] Reduce code duplication in PythonCommonCompletionTest
GitOrigin-RevId: 1e39bed5d00d3253907dc0d8cedbd37d323a9859
2024-04-25 23:00:28 +00:00
Mikhail Golubev
cef42660a3 PY-17627 PY-17733 Resolve class attributes defined in @classmethods
The logic is similar to that for instance attributes. Top-level class
attributes and methods defined in the class body get the precedence,
followed by class attributes defined with assignments in @classmethods
unless the latter would resolve to the same assignments as in

cls.attr = cls.attr + 1

finally, we scan through all other class methods resolving the name
to the first definition inside one of them.

So far, I intentionally didn't expose such attributes in findClassAttribute()
or getClassAttributes() because users of these methods assume that
this API considers only attributes defined immediately in the class body.
Adding extra definitions from class methods might break these usages.

I had to update the inspection about typing.Final, because it relied
on the fact that resolve() on assignment targets on class objects can
lead only to those top-level class attributes, where type hints are normally
located, but now it can lead to assignments to a qualified attribute inside
a containing class method.

GitOrigin-RevId: 0ca5bdaa4efca127ac187e822a49df6795e1028a
2024-03-28 12:17:37 +00:00
Daniil Kalinin
b83be81bb7 PY-61878 Resolve type parameters and type aliases on stubs
Do not resolve type parameters as class members

Tests for the changes above

Co-authored-by: Mikhail Golubev <mikhail.golubev@jetbrains.com>

GitOrigin-RevId: 96309ebedf26d04e375bfa3a5f8ae0bc9257d48f
2023-11-06 19:59:18 +00:00
Mikhail Golubev
98016a1ae6 PY-63367 PY-63366 Include instructions for annotations of new-styles generic functions in their own CFG
This way they have access to type parameters of these functions, and PyReferenceImpl.getResultsFromProcessor
can properly perform flow-sensitive resolve for them now that READ instructions for expressions
inside annotations and WRITE instructions for type parameters are in the same CFG. It allowed to
remove all the special logic for type parameters from PyReferenceImpl as well as from
PyUnboundLocalVariableInspection.

I had to special-case these annotations in PyResolveUtil.scopeCrawlUp, though, to "lift" their
original scope back from what ScopeUtil.getScopeOwner returns, making names defined in a class scope,
e.g., nested classes, visible to them.

GitOrigin-RevId: b2e78211565300dbcd7c2e2b246a7a8e14bb0e8f
2023-10-16 23:42:44 +00:00
Mikhail Golubev
8a5536359d [python] Simplify PyCommonResolveTestCase.assertNotResolved
GitOrigin-RevId: 26bd034de5e26ec2438f9e98a4ce648f96f04707
2023-10-05 22:02:21 +00:00
Daniil Kalinin
97185d17c8 PY-62608 PEP 695 Type Parameter Syntax: Resolve and scoping for type parameters in type aliases
GitOrigin-RevId: be532456e7a9c470d5d0a2770a7a2eb9b9b6e8de
2023-09-28 15:05:33 +00:00
Daniil Kalinin
3a70f1d22d PY-61854 PEP 695 Type Parameter Syntax: Formatting
GitOrigin-RevId: 272f956033d82626d7cf689aa561cdeab3f951da
2023-09-15 16:04:46 +00:00
Olga.Lavrichenko
c9b1045a46 DS-5427 DS-5549 Fixed parsing magic commands after empty cells
GitOrigin-RevId: d2359bacd7f772ebeaaabe7d1d43da060d2f7928
2023-09-12 13:48:51 +00:00
Mikhail Golubev
63d24cbb9d PY-34493 Enable back copying annotations from third-party .py files on Override/Implement
Since disabling turned out to be controversial. Even in the ticket some users were
against this decision claiming that it heavily annotated code bases it might be necessary
to retain all annotations from inherited method signatures. We need to think of a better
solution for controlling this behavior on case-to-case basis.

GitOrigin-RevId: c68763f0cad23de8975085e9bc8a6fd99013de3b
2023-08-09 20:53:35 +00:00
Mikhail Golubev
a2af264b63 PY-45588 Add necessary imports on completing methods of superclasses to override
I had to move addImports to PyClassRefactoringUtil because PySuperMethodCompletionContributor
resides in python-psi-impl and, thus, has no access to PyOverrideImplementUtil.

GitOrigin-RevId: cf2ac19da779977649144b2477bac3f8ae78bbcd
2023-08-09 20:53:35 +00:00
Mikhail Golubev
b50c617b7c PY-34493 Don't copy annotations from .pyi stubs and libraries on super method completion
GitOrigin-RevId: 65787827f5df5aa80986107dda0ba555b0942d40
2023-08-09 20:53:35 +00:00
Olga.Lavrichenko
d4df46d5f0 DS-4478 Reformat code removes spaces in shell commands
GitOrigin-RevId: b06215e01de70329f3c9dfef4ea330ea73400cd2
2023-07-24 17:24:56 +00:00
Daniil Kalinin
2983980cbd PY-17193, PY-10709 enable Use parentheses instead of backslashes for breaking lines option by default
Change tests according to the new default value

GitOrigin-RevId: 79a09911127532fadcc4a4d37a454887d46effa9
2023-07-07 13:26:55 +00:00
Olga.Lavrichenko
6df5bfd10d DS-1584 DS-2583 Reformat breaks IPython magic commands and shell commands
GitOrigin-RevId: 949528c951d6c9174b1bad21452a90fbdb07b418
2023-01-16 15:13:23 +00:00
lada.gagina
01f87f5c77 PY-42637 Fix quotes for TypedDict keys completion in subscription expressions
If there are quotes, suggest just the key names

GitOrigin-RevId: 58caea6ba1ceeeea790580c3d24d255a636764bb
2023-01-10 17:39:28 +00:00
Petr Golubev
68f080743c move python formatter extensions to python.psi.impl module
GitOrigin-RevId: 452346cbd12e9c21c636dc8dbcab9a29813bc67b
2022-11-03 14:27:33 +00:00
Irina.Fediaeva
181edc5caa PY-50788: Correct resolve for inherited docstring attribute names
Now during resolve if there is no class/instance attribute with appropriate name explicitly in the class, we will try to find it in the parent classes and resolve there.

GitOrigin-RevId: 52ab5f0ad63cba187457ba3db6107997ede33dee
2022-10-03 13:21:34 +00:00
Tagir Valeev
adb060863f Text blocks used
GitOrigin-RevId: 7db538c0a10131a3f946436c85b42fe7d7dc5b10
2022-09-29 12:58:29 +00:00
Irina.Fediaeva
cce0ef17d5 PY-16760, PY-28549, PY-35743, PY-55609, PY-46654: Correct resolve of class attributes in docstrings
Previously in docstrings we incorrectly recognized references in 'Attributes' section only as instance attribute references. It led to false positive 'Unresolved reference' on class attributes references in docstrings and wrong resolve when using class and instance attributes with the same names. Now we use ReferenceType.VARIABLE to identify both class and instance attribute references in 'Attributes' section and then resolve them with priority of instance attributes. Also fixed wrong resolve of attributes references to constructor parameters.

GitOrigin-RevId: ea10bfb092472c0ab14b77f06efd93093cfcd684
2022-08-25 21:11:57 +00:00
Alexey Kudravtsev
85895e8989 add more SSR: replace stream expression with ContainerUtil call
GitOrigin-RevId: 629f61b8d4541fb374dedcbbce2964ddb70dfdf1
2022-08-24 11:15:49 +00:00
Daniil Kalinin
fb11ee327c PY-53200 do not autocomplete parameter list if method already have one
Test for PY-53200

Better tests for PY-53200

PY-53200 Removed unnecessary test.

`testMethodNamesDoNotDuplicateParameterList` renamed and moved closer to the corresponding ones
Removed test for method names in suggestions restored

GitOrigin-RevId: 51b0721190718c44810c16e198bad583c2af67d0
2022-05-05 11:18:29 +00:00
Semyon Proshev
7b8b5bb12f Make creating resolve context with explicit type eval context shorter
GitOrigin-RevId: 7dce0f1869ca114d729f8368273291940eda21ef
2021-06-13 14:50:11 +00:00
Dmitry Jemerov
749ce8a332 Cleanup: remove @author tags
GitOrigin-RevId: 9174eb8d77215b16f9f6c6074e6aced654d95ef1
2021-05-03 13:07:48 +00:00