Commit Graph

4489 Commits

Author SHA1 Message Date
Sergey Karpov
fc1db76c4d [pycharm] PY-40180. Gave the test a better name, added a comment with issue ID.
(cherry picked from commit 908a293fa325205be5fc902d788a26b4ed4013b2)

IJ-CR-124368

GitOrigin-RevId: d95a4345437e9d9f8d98def50fec2338b0a7aabc
2024-03-07 12:55:53 +00:00
Sergey Karpov
6ae3e4f160 [pycharm] PY-40180. Renamed the test to reflect the latest LanguageLevel usage.
(cherry picked from commit 3dcc9144326ad66284b0ac250feb6433848d8b28)

IJ-CR-124368

GitOrigin-RevId: 4b33d1ef38e0180bd0b4080647718f880c127802
2024-03-07 12:55:48 +00:00
Sergey Karpov
e371768835 [pycharm] PY-40180. Moved the check inside checkReturnValueAllowed. Added a regression test.
(cherry picked from commit 0367ac9bba083eb46f5759f7fa102cf1545215be)

IJ-CR-124368

GitOrigin-RevId: 0998aaaa50d2d8b6efc49295b22c2245f5477878
2024-03-07 12:55:42 +00:00
Egor.Eliseev
31ede1b27d PY-34498 Add an inspection for pytest fixture that is not passed to test parameters
Fix incorrect fixtures searching in imports. Add test that inspection registers problem only for PyTestFixture.

GitOrigin-RevId: de029bb401689f0218e6fce04e64e738a8051fae
2024-03-05 14:01:54 +00:00
lada.gagina
92e8282dea [python] requirements.txt: Add tests for missing packages inspection (PY-65403)
GitOrigin-RevId: 5183d3b00b640510325c96a6c996cb308fe5b188
2024-02-26 03:26:38 +00:00
lada.gagina
e97eeace5c [python] requirements.txt: Add an in-place inspection for not installed packages (PY-65403)
GitOrigin-RevId: 29d5b17ce5946c807afa635ac11ea9bdaf200999
2024-02-26 03:26:30 +00:00
Alexey Kudravtsev
dfc5db43aa optimization: filter inspection by language more correct in tests
Provide correct EP for inspection tools even with inconsistent tool.getShortName() and shortName="" in plugin.xml.
That allows obtaining correct tool.getLanguage(), and avoid running irrelevant inspections.
E.g. CheckDtdRef inspection doesn't run in java-only tests anymore.

GitOrigin-RevId: 188e9d55686ca084611c5c89cb899874dd078010
2024-02-25 04:22:20 +01:00
Mikhail Golubev
7320815ad2 PY-65385 Retain unbound ParamSpecs as-is during type parameter substitution
The original problem with @contextlib.asynccontextmanager was due to a bug
in PyTypeChecker.substitute introduced in the TypeVarTuple support. Namely,
we started to substitute unmatched ParamSpec types with null, effectively
replacing them in a callable signature with a single parameter of type Any.
Then the special logic in PyCallExpressionHelper.mapArguments that treated
unmatched ParamSpecs as "catch-all" varargs stopped working, and we started
to highlight all extra arguments in the substituted callable invocations.

In other words, binding type parameters from decorator targets, e.g.
ParamSpecs or function return types, never worked because we can't resolve
functions passed as decorator arguments in "de-sugared" expression fragments
in the codeAnalysis context, i.e. when we replace

```
@deco
def f(x: int, y: str): ...
```

with `deco(f)` and then try to infer its type in PyDecoratedFunctionTypeProvider,
but we didn't report it thanks to that special-casing of unmatched ParamSpecs
(other type parameters replaced by Any don't trigger such warnings).

Ideally, we should start resolving references in arguments of function calls
in such virtual expression fragments in some stub-safe manner instead of relying
on this fallback logic. In the general case, however, complete stub-safe inference
for decorators is a hard problem because arbitrary expressions can affect types of
their return type, .e.g.

```
def deco(result: T) -> Callable[[Callable[P, Any]], Callable[P, T]]: ...

@deco(arbitrary_call().foo + 42)  # how to handle this without unstubbing?
def f(x: int, y: str): ...
```

GitOrigin-RevId: adeb625611a3ebb7d5db523df00388d619323545
2024-02-19 16:29:50 +00:00
Artem Mukhin
4c5a6ccc13 [Grazie] Hotfix failing spellchecker tests
https://jetbrains.team/p/ij/reviews/126070/timeline

GitOrigin-RevId: 185a4e392e2d09ccdab5c515ca32895d56f3d77b
2024-02-17 02:46:52 +00:00
Egor.Eliseev
5ef4bd59e7 PY-30279, PY-40318 Add correct type resolve for async and iterated fixtures
Merge-request: IJ-MR-108198
Merged-by: Egor Eliseev <Egor.Eliseev@jetbrains.com>

GitOrigin-RevId: dfd6501f64be944bff21621bce6ef45218912337
2024-02-13 20:54:32 +00:00
Alexey Kudravtsev
394e5c640a IDEA-321013 Outdated HighlightInfo's should be removed as soon as the relevant annotator or inspection finishes
Rework annotators engine to make annotators run in parallel, each on all relevant PSI elements in their own order (makes fast annotators complete faster to allow them to remove outdated highlighters faster).
For that, for each annotator (in parallel):
- create its own AnnotationHolder
- rearrange its PSI elements in "time to first diagnostic in previous run" order, to reduce latency.
- run annotator on these PSI elements sequentially
- as soon as annotator produces info/fails to produce the same info from the previous run, update the corresponding range highlighters
Pleas note, there's no more contract "Do not call annotators for parent PSI if some (maybe completely unrelated) annotator/highlight visitor produced error for some PSI element".
Fix highlighting tests, the majority of which relied on annotator order, or implicit contract above.
Fix a bunch of annotators which tried to double-visit some PSI elements to fight the contract above.

GitOrigin-RevId: 74f727fc6d3be3f500cdbb0f26e7d0daf1ffe7ff
2024-02-12 18:52:52 +00:00
Gregory.Shrago
f90dab5aae make EditorTestFixture.type work as in production
Perform real shortcut processing as in production environment.
Must not be called in WA due to that.

GitOrigin-RevId: faa0302c4cd7460f08792e6170ae027cbd415de4
2024-02-09 21:23:08 +00:00
Mikhail Golubev
454deac682 [python] Fix PyStringConcatenationToFormatIntention for enclosing string concatenations
Noticed in IJ-CR-123777.

GitOrigin-RevId: f32832bca10e805fc609babf81382709d58fc480
2024-02-07 17:34:49 +00:00
Mikhail Golubev
b55fd53258 PY-35627 Make Python formatter always add a line break after a decorator list
For regular non-async functions, it already worked thanks to PyTokenSeparatorGenerator.

GitOrigin-RevId: b60dba7b34710cad7dc46ad6f943b3944fc00c42
2024-02-07 17:34:28 +00:00
lada.gagina
e7a2d7c816 [python] Add tests for poetry dependencies completion
GitOrigin-RevId: 193423fca2e21d9e3d7620de9ca14580cd3a512c
2024-01-24 19:42:07 +00:00
Daniil Kalinin
c79da2708c PY-64074 Add Quick Documentation for type parameters and type alias statements
Merge-request: IJ-MR-119974
Merged-by: Daniil Kalinin <Daniil.Kalinin@jetbrains.com>

GitOrigin-RevId: fa0c57b3005b31d892a394b3a5f595ac10135a71
2024-01-23 09:50:49 +00:00
Petr Golubev
05484d8f97 PY-53120 "Add method" action does not create async def
GitOrigin-RevId: 8b2286a3f50d7b0d00ebbb0befc9811a80f7b304
2024-01-19 20:06:59 +00:00
Mikhail Golubev
f9bb539137 [python] Speed up and extend PyVariableAnnotator
Traversing through all children of a PyFunction looking for nested functions,
possibly declaring a nonlocal or global variable, and doing that for every
target expression or a reference to one, is both inefficient and seemingly
hardly needed. For a name being local means that it cannot be accessed
outside the scope of the corresponding function. Updating this variable from
some inner helper function doesn't violate this property. In Python, one
has to explicitly mark a name from an enclosing scope as nonlocal or global to
be able to assign to it within a function. It seems enough (and less surprising)
to rely on this information to distinguish between local variables and everything
else.

All in all, if some local variable is accessed as a nonlocal name in an inner
function, it's now still highlighted as local in the function that defines
it (previously it wasn't), but it is not in the function declaring it as
nonlocal (same as before).

Additionally, we now uniformly highlight as local variables "for" and "with"
statement targets, targets of assignment expressions, names bound in patterns,
and variables in assignments with unpacking (previously it was done only for
trivial assignments).

GitOrigin-RevId: 04c07ae6814a6b531911b3d87a3a26191c934962
2024-01-12 11:52:08 +00:00
Andrey Lisin
1199e0bdaa PY-36882 Fix debugger not working properly with joblib.Parallel when using loky backend
GitOrigin-RevId: 392e04e2e10ae404caf8c561a7c951e7eaea3c9c
2024-01-11 18:52:11 +00:00
Mikhail Golubev
fa168f4a00 PY-36444 Remove @contextlib.contextmanager from PyKnownDecoratorUtil.KnownDecorator
This decorator is fully type hinted in Typeshed, so, with the changes introduced
for PY-60104, it's no longer necessary to special-case it anywhere.
PyDecoratedFunctionTypeProvider can infer the correct type after application
of this decorator to a generator function just as for any other typed decorator.

The original problem was caused by the fact that PyDecoratedFunctionTypeProvider
didn't process declarations having any decorator listed in the KnownDecorator enum,
as presumably all of them were too "magical" to analyze.

Co-authored-by: Daniil Kalinin <daniil.kalinin@jetbrains.com>

GitOrigin-RevId: 53b277803a1eb42784131d0dae5bb7ace173c017
2024-01-09 20:49:13 +00:00
Mikhail Golubev
9300fe8c45 PY-60104 Clean up in tests about type inference for decorators
Removed a number of tests in Py3TypeTest duplicating those of PyDecoratedFunctionTypeProviderTest.

Removed the tests about PY-23067 in Py3ArgumentListInspectionTest and Py3CompletionTest because
this issue was actually not addressed in 05e8ed4df0c7faa24bd972e1b422f664d708b510, and the behavior
some of them assert is not what users wanted.

More consistent naming of tests in PyDecoratedFunctionTypeProviderTest and PyParameterInfoTest.
Removed there excess tests too similar to others or checking scenarios not relevant to
the current approach to type inference for decorators, e.g. presense of @functools.wraps and
alternatives inside decorators — we don't even analyze their bodies anymore.

Add a few extra tests illustrating problems with the current approach:
- testNotAnnotatedDecoratorChangingFunctionSignatureIsIgnored
- testInStackOfDecoratorsChangingFunctionSignatureOnlyAnnotatedAreConsidered
- testInStackOfImportedDecoratorsChangingFunctionSignatureOnlyAnnotatedAreConsidered
- testNotAnnotatedDecoratorRetainsParametersOfOriginalFunctionEvenIfItChangesItsSignature

GitOrigin-RevId: 0bf5070fc523b88dcc9d3009786dd028bdfa0feb
2024-01-09 20:49:13 +00:00
Mikhail Golubev
3079150697 PY-60104 Don't try to infer side effects of not type hinted decorators
Assume that such decorators as well as "well-known" decorators, which we special-case,
don't change signatures of decorated functions and classes.

This change effectively stops the long-standing policy of safe-listing a few recognized
"well-known" decorators and assuming everything else can change a definition in any
way. This approach doesn't apply well to the current state of the Python world, where most
of the common side effects of decorators, such as adding new parameters, can be expressed
in type hints.

In 2021.1 we added PyDecoratedFunctionTypeProvider that was able to infer a return type of
decorator over its body, as for any other function, and then correctly apply this information
to a decorated definition. It led to a number of problems.

First of all, depending on whether TypeEvalContext allowed us to access AST of a decorator's
body, we inferred different signatures for functions decorated with an imported decorator in
inspections and in user-initiated actions, such as Parameter Info.

Secondly, we started inferring useless `(*args, **kwargs)` signatures in case of decorators
defined following the common pattern of returning a wrapper function accepting arbitrary
parameters and itself decorated with @functools.wraps (PY-48338). In some sense, our code
analysis was "too smart" in its type inference in this case.

Lastly, we diluted the return types of functions decorated with unknown decorators, even
fully typed, by uniting these types with Any (so-called "weak" types). This logic
existed before PyDecoratedFunctionTypeProvider, but it became more problematic now
than we were able to propagate this artificial union through generic decorators.

This change in behavior might lead to some false positives for untyped Python code
with non-pure decorators. However, given that other type checkers are also likely to hit these
problems, there is now a stronger incentive to add type hints for such problematic APIs.
In the worst case, we can special-case some heavily requested decorators as we did before.

GitOrigin-RevId: db11fb3573bda5da155cb921a30adc31d5c841e2
2024-01-09 20:49:13 +00:00
andrey.matveev
52f21cc60d PY-48338 PY-60104 Make decorators pure functions if not annotated
GitOrigin-RevId: fed5c22c9114a5dcfb882be367c570baf8ba3e5a
2024-01-09 20:49:13 +00:00
Andrey Lisin
d51a42a60f PY-65353 Add test
GitOrigin-RevId: 9e8beb9b56fae71040e312ab92f6d09e56f6395b
2024-01-09 15:01:02 +00:00
lada.gagina
e5166b993e [python] Inject requirements language into pyproject.toml (PY-42296, PY-54440)
GitOrigin-RevId: 196f56270d0a102a074872871898d640d77b2918
2024-01-08 16:36:32 +00:00
Andrey Lisin
fa03d3e322 PY-37366 Add test
GitOrigin-RevId: c5ca2bfe4cd9a8c9e30deccd5658a71a317e9b23
2024-01-04 18:55:51 +00:00
Egor.Eliseev
e9802d2cb8 PY-62172 Add support for imported using wildcard pytest fixture resolving
Merge-request: IJ-MR-122222
Merged-by: Egor Eliseev <Egor.Eliseev@jetbrains.com>

GitOrigin-RevId: f6baf79341bb54c28b2ef6f3faaafa4385266e05
2023-12-14 13:39:07 +00:00
Egor.Eliseev
0aecae551e PY-62419 Pytest fixtures from pytest_plugins are not resolved
Add fixtures searching in `pytest_plugins` statement from `conftest.py`
Add tests


Merge-request: IJ-MR-121756
Merged-by: Egor Eliseev <Egor.Eliseev@jetbrains.com>

GitOrigin-RevId: 673ad9a614dba84ffdf87398f908a54af02be81f
2023-12-08 10:44:56 +00:00
Mikhail Golubev
44826d3452 PY-64481 Select __iter__ overload for a for loop/comprehension target matching "self" type
GitOrigin-RevId: 7f3cbdc1d3a6a1fb633d2944567a968b88bfaacd
2023-12-05 17:41:30 +00:00
Petr Golubev
d884d84520 PY-40923 Create function intention does not recognized async invocations
GitOrigin-RevId: d1c1c2891ce66c3d2ae7faafc6683e40d772304e
2023-12-05 13:31:18 +00:00
Daniil Kalinin
99bca18eb1 PY-63856 Add info about TypeVar bounds into the inspection message about unmatched types in PyTypeCheckerInspection.java
Merge-request: IJ-MR-120117
Merged-by: Daniil Kalinin <Daniil.Kalinin@jetbrains.com>

GitOrigin-RevId: fdf3c6fd734f69f82595bb51ce5badc437ca5045
2023-11-28 14:51:31 +00:00
Daniil Kalinin
409ee5968e PY-61858 PEP 695 Type Parameter Syntax: Add Language injections inside quoted forward references
GitOrigin-RevId: f38d5d5d3ae9529e2ffb8ddd252f399fb6cea7bd
2023-11-27 12:16:54 +00:00
lada.gagina
e51528cc47 [python] Add tests for requirements.txt completion for package names and versions
GitOrigin-RevId: 8fc3931d8c3e022264253d8bb9bc0d43a3b6f557
2023-11-23 23:18:30 +00:00
Mikhail Golubev
910a39d81f PY-64124 Match expected positional- and keyword-only parameters with regular parameters
The original false positive was caused by the protocol NestedSequence in matplotlib
defined as:

class NestedSequence(Protocol[_T_co]):
    def __getitem__(self, key: int, /) -> _T_co | NestedSequence[_T_co]: ...
    def __len__(self, /) -> int: ...

which was used to annotate parameters of matplotlib.pyplot.plot and was considered
incompatible with the builtin list.

Skipping positional- and keyword-only parameter separators of the expected callable
is a workaround until we have a comprehensive mechanism for matching signatures.

GitOrigin-RevId: 93d8bb4c6c4405d0e24b5f98152a461691f6197e
2023-11-23 00:51:33 +00:00
lada.gagina
75b1ecd244 [python] Cleanup supported/unsupported versions xmls
- Drop support for python 3.5 & 3.6 in compatibility inspection
- Fix and remove some outdated tests
- Remove xmls for long-unsupported python 2.6 & 3.5
- Regenerate versions.xml
- Remove mentions of OS-specific modules

GitOrigin-RevId: 3265dd1de8a4f7a41119e10c95bb705ca5845efe
2023-11-17 18:57:31 +00:00
Andrey.Matveev
db5c251333 PY-63273 Fix tests
GitOrigin-RevId: 6c26cc44b2ab1080cb3c1d61b7a7053622ce0e51
2023-11-10 17:48:24 +00:00
Andrey.Matveev
9484e67247 PY-63273 Fix testPathLikePassedToStdlibFunctions
GitOrigin-RevId: ea8c740012d6f37f03c78383e691ce6f748fa976
2023-11-10 17:48:23 +00:00
Andrey.Matveev
e8baba5690 PY-63273 Fix testBuiltinsPy3 test
GitOrigin-RevId: aec045631681e7d056eaaddbacc30e59ba53881d
2023-11-10 17:48:23 +00:00
Andrey.Matveev
52229081e9 PY-63273 Fix tests related to TypeVar constructor
GitOrigin-RevId: 2608401a0a9b35d458b923ccb46d2e140e9abedf
2023-11-10 17:48:23 +00:00
Andrey.Matveev
fe1d064d41 PY-63273 Remove source from NAMEDTUPLE_SPECIAL_ATTRIBUTES
GitOrigin-RevId: f4744a3818835e14e4ce41034bec13f931a2aab6
2023-11-10 17:48:23 +00:00
lada.gagina
4061c6e187 PY-61139 Do not infer LiteralString for parameters with str default value
(cherry picked from commit 80992fa5d2d3e393326e4f469fe3bd6472f70643)

IJ-MR-112631

GitOrigin-RevId: ebbe6c433c4536660c7ccbe8462f16667a75c434
2023-11-07 22:10:30 +00:00
lada.gagina
ad5b55918e Revert tests with over-inferring LiteralString
(cherry picked from commit 0c92e1c64362e2834b01b4e0bcbcedfefe4febe0)

IJ-MR-112631

GitOrigin-RevId: 233abfde16b19e96c5bc39c77eef1a240aa1b3e8
2023-11-07 22:10:23 +00:00
Daniil Kalinin
ac6316198f PY-61857 Implement PyTypeParameterListAnnotator for PEP 695 type parameter syntax
It reports such cases:
* Duplicated type parameter names in type parameter lists
* Wrong number of type var constraints (one or zero) defined with new-style PEP 695 syntax
* Use of constraints for ParamSpec and TypeVarTuple type parameter kinds

GitOrigin-RevId: e0e8e7eb4dcef0c1b56ea49a3527666e3c713d86
2023-11-06 19:59:18 +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
Daniil Kalinin
eb58a3805e PY-61883 PEP 695 Type Parameter Syntax: Make PyTypingTypeProvider aware of the new-style type parameters and aliases
Inherit PyTypeAliasStatement from PyQualifiedNameOwner to re-use type aliases stack in PyTypingTypeProvider

Various tests for the changes above

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

GitOrigin-RevId: 242427c6f84c05ec48c94085f20675b8e30f8625
2023-11-06 19:59:18 +00:00
Daniil Kalinin
0ca9ba4a99 PY-62608 Store info about type parameter kind (TypeVar/ParamSpec/TypeVarTuple) inside PSI stubs
GitOrigin-RevId: 80aa6d67e32a0e7e2a7e8ed53f72288831e9dccf
2023-11-06 19:59:18 +00:00
Andrey.Matveev
c24752ceb1 PY-58752 Impl inspection for check matching override signature
GitOrigin-RevId: 4520138ac657a514b53f2f939521f0336701be46
2023-10-22 10:27:24 +00:00
Mikhail Golubev
ffda93c6fb PY-62641 Fix promotion of collection types to their versions containing literal type parameters
Previously, we incorrectly inferred generic list and set types parameterized with
types of each element of the corresponding collection literal instead of a union
of those. For instance, for [1, 2] we inferred list[Literal[1], Literal[2]] instead
of list[Literal[1, 2]].

GitOrigin-RevId: 6f222daee871137a5de5589429f78341704c5544
2023-10-20 13:38:08 +00:00
Mikhail Golubev
a14c9ef92c PY-53105 Support PEP 646 and TypeVarTuples. Take 2.
The introduction of TypeVarTuples and the concept of unpacked tuple types made us
revise all the places where we match sequences of types in type inference.
For instance, when matching type parameters and type arguments for generic
specialization in:

* type hints, i.e. xs: MyGeneric[int, str] = MyGeneric()
* constructor invocations, i.e. xs = MyGeneric[int, str]()
* class declarations, i.e. class MyGeneric(Base[T1, T2, str]): ...
* type alias declarations, i.e. MyAlias: TypeAlias = MyGeneric[T, int]

as well as during type matching of all generic types, both normal non-variadic and
existing "built-in" generic variadics in the type system, namely tuples and
Callables.

Previously, this logic was spread across numerous places in PyTypeChecker and
PyTypingTypeProvider, all with their own subtle differences. The first attempt
of PEP 646 support put all the code for uniform matching of type parameters directly
in PyTypeChecker, significantly complicating its already arcane internals.
I've introduced a unified API for that called PyTypeParameterMapping.
It still retains some of the former quirks in form of its Option flags, controlling
in particular how we handle having some of the expected types unmatched
(imagine expecting MyGeneric[T1, T2, *Ts] and receiving MyGeneric[int]),
but I'm planning to gradually eliminate this conditional logic.

The same class is now also responsible for matching parameter types of callables
that already allowed to fix some of the known problems, such as ignoring their
arity (PY-16994), but I'm going to extract a separate API entity for that, since
matching of callable signatures is a much more complicated task involving
compatibility of different types of parameters (positional-only, keyword-only,
defaults, varargs, etc.).

Another positive side effect of these changes is that substitution of type
parameters during type inference became more consistent, and we no longer lose
useful type information by replacing all unbound type parameters with Any. It's
particularly visible in type checker errors where we stopped dropping unbound type
parameters from messages about mismatched parameter-argument types.

Among other improvements in this changeset are proper scoping for
TypeVarTuples, consistent with other type parameters, and recognizing TypeVarTuples
and unpacked tuples in types of *args parameters in function bodies, e.g.
`*args: *Ts` translates to "args" parameter having the type `tuple[*Ts]`.

Confusing PyNoMatchedType used only for reporting of missing arguments for *args
parameters annotated with unpacked tuples in the type checker inspection, e.g.

def f(*args: *tuple[int, str]): ...
f(42)  # a type checker error about a missing argument for str

was also removed from the type system in favor of a simpler approach with handling
such errors directly in the inspection. We might need such a general type in
the future, but it has to be well thought-through.

GitOrigin-RevId: 63db6202254205863657f014632d141d340fe147
2023-10-20 13:38:06 +00:00
andrey.matveev
a75c569022 PY-53105 Support TypeVarTuple
GitOrigin-RevId: f8160e9d802b09991daa710ed9a20f3e30d455da
2023-10-20 13:38:04 +00:00