Commit Graph

151 Commits

Author SHA1 Message Date
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
caf1082077 [python] Remove instructions for parameter defaults and return type annotations from functions CFG
Both of these are evaluated in the outer scope, e.g., in a scope of a containing class or a module,
with the corresponding instructions already processed there, and were duplicated in the CFG
of function bodies.

GitOrigin-RevId: 4460cbef6446a311f0ab15ffe3407bfbb73286e2
2023-10-16 23:42:44 +00:00
Daniil Kalinin
65cc3ddfd0 PY-61877, PY-61878 PEP 695 Type Parameter Syntax: Control flow for type parameters and type aliases
GitOrigin-RevId: 2db381cbb97891296bae09e48c17b46eefa57a04
2023-09-28 15:05:31 +00:00
Vladimir Koshelev
7aeb74fe0a [PY-49040] add support for TypeGuards
Merge-request: IJ-MR-109904
Merged-by: Vladimir Koshelev <Vladimir.Koshelev@jetbrains.com>

GitOrigin-RevId: 8441ce35b2fc97fb0cdaf747feff2cf9ba3347ea
2023-08-02 22:27:54 +00:00
Daniil Kalinin
45bb1fffb8 PY-24273, PY-53703 Support for functions annotated with typing.NoReturn and typing.Never
Functions annotated with `NoReturn` and `Never` now taken into account in the Control Flow Graph building process, and the code after calling such functions is treated as unreachable.

Merge-request: IJ-MR-105973
Merged-by: Daniil Kalinin <Daniil.Kalinin@jetbrains.com>

GitOrigin-RevId: ef5840ae6e593498fc334dc9bd2daadccebf2b13
2023-06-13 22:08:30 +00:00
Daniil Kalinin
7842a42c51 PY-41231, FL-15391 Ignore type annotations in Python live templates
Merge-request: IJ-MR-102294
Merged-by: Daniil Kalinin <Daniil.Kalinin@jetbrains.com>

GitOrigin-RevId: 753721489edb85702b72d56da1016bbc85af9f52
2023-02-09 16:59:39 +00:00
Daniil Kalinin
c245993809 PY-7758, PY-23859 improvements in Control Flow analysis
Control flow now abrupts on `exit()` and `pytest.fail()` calls

Control flow now abrupts only if class which contains `self.fail()` call contains case-insensitive "test" word in the name 

Merge-request: IJ-MR-96165
Merged-by: Daniil Kalinin <Daniil.Kalinin@jetbrains.com>

GitOrigin-RevId: ea173fdb72a10a373cd95f266ea7589e36545f30
2022-11-01 09:54:20 +00:00
Daniil Kalinin
93e169eea2 PY-26060 New version-dependent super method signature macro
Tests for PY-26060

Merge-request: IJ-MR-95531
Merged-by: Daniil Kalinin <Daniil.Kalinin@jetbrains.com>

GitOrigin-RevId: 134f45d43aefc3cf7a3f806e0ea1ea6f768528f4
2022-10-14 14:02:44 +00:00
Daniil Kalinin
fa259d0727 PY-52162 deprecated implementation of PythonTemplateContextType#isInContext changed to accept TemplateActionContext
Tests for PY-52162

Merge-request: IJ-MR-24510
Merged-by: Daniil Kalinin <Daniil.Kalinin@jetbrains.com>

GitOrigin-RevId: 8cb7674b3840424015eeb06981ec16734fe26af2
2022-05-20 08:37:03 +00:00
Mikhail Golubev
97b22aaa13 PY-42200 Support parenthesized context managers in Python 3.9+
In case of syntactic ambiguity with previous versions of the grammar, such as
"with (expr)" or "with (expr1, expr2)", PyWithStatement is still parsed as
having its own parentheses, not a parenthesized expression or a tuple as
a single context expression. The latter case, even though syntactically legal,
is still reported by the compatibility inspection in Python <3.9.

These changes also include proper formatter and editing support (e.g. not
inserting backslashes on line breaks inside parentheses), as well as
Complete Current Statement, which now takes possible parentheses into account
while inserting a missing colon.

The changes in the formatter are somewhat ad-hoc, intended to minimize the effect
on other constructs. "With" statement is somewhat special in the sense that it's
the first compound statement (having a statement list) with its own list-like
part in parentheses.

Existing tests on with statement processing were expanded and uniformly named.

Co-authored-by: Semyon Proshev <semyon.proshev@jetbrains.com>

GitOrigin-RevId: 15c33e97f177e81b5ed23891063555df016feb05
2022-05-04 12:31:37 +00:00
Mikhail Golubev
20097f7a69 PY-49785 Properly recover from "case" keyword in the middle of a match statement
Not stopping at a statement break token and continuing recovery until a colon,
we considered the subsequent well-formed case clause to be a part of an error
message about a missing pattern, thus, moving the caret to its colon.

GitOrigin-RevId: f4ee0e12876960e989de3dee89925b65e3cf2339
2021-07-19 16:29:15 +00:00
Mikhail Golubev
353d7f24d1 PY-48014 Implement Complete Current Statement for match statement and its clauses
GitOrigin-RevId: dba29d39fc90397a2f4af8a347cfcbf208ce5c92
2021-06-24 15:04:01 +00:00
Mikhail Golubev
cb08d4de98 PY-48760 Implement CFG for PEP-634 match statements
I introduced a new type of CFG instructions, similar to ConditionalInstruction,
called RefutablePatternInstruction. The idea is that every pattern that can
possibly fail to match is surrounded with a pair of such instructions, helping
to describe how the control flow moves in each case. The PEP calls the opposite
type of patterns that always match "irrefutable", hence the name. We need these
synthetic instructions because otherwise some refutable patterns such as literal
ones (e.g. "42") don't leave any nodes in the graph. Incorporating the information
about irrefutable patterns right into the graph allows catching cases such
as "wildcard/name capture makes remaining patterns unreachable", both in OR
patterns and independent case clauses.

GitOrigin-RevId: beebe1890a6a824b188e6954a2c92f7ec52079e0
2021-06-11 10:16:38 +00:00
Mikhail Golubev
7c226889fc Preserve the capitalization in test data files of PyControlFlowBuilderTest
Previously, it used an odd convention with lowercased names of test data
files (with nothing separating individual words), which made adding new tests
quite tedious.

Also, I removed the test data "exit.py" as it wasn't used by any test.

GitOrigin-RevId: 92b1963c67ed01977f5b7ad020984056c9fe045d
2021-05-11 15:55:26 +00:00
Semyon Proshev
ea9c2c9415 Reuse if-statements approach for while-statements to understand when and where condition is succeeded/failed (PY-39262)
It allows to assume that under `and` condition all sub-conditions are satisfied.

GitOrigin-RevId: f58ed2706d7eb9c1595415cce57cb8020011b57a
2021-03-23 11:45:13 +00:00
Semyon Proshev
882c5348b3 Add separate CFG instructions to if-statements to understand when and where a condition is succeeded/failed (PY-39262)
It allows to assume that under `and` condition all sub-conditions are satisfied.

GitOrigin-RevId: 104bb5a2200791d183cc26e008b974a6b658fa37
2021-03-23 11:45:12 +00:00
Andrey Vlasovskikh
a79f1a9dd9 PY-43889 Fixed capitalisation in names of test data directories
GitOrigin-RevId: 145ab5cf3f9d990112b722ea8f97564a88631f59
2021-03-09 14:48:46 +00:00
Andrey Vlasovskikh
ff7ead9b79 PY-43889 Expand 'main' live template only at top-level
GitOrigin-RevId: bcf272b5032a6f9ee4143a38cf7cc6da965053f4
2021-03-08 23:05:55 +00:00
Mikhail Golubev
13afc2cbfb PY-43053 Handle Complete Current Statement after a collection at the end of a file
It's weird, however, that Python implementation might query fixers for elements
that are at completely different lines with the caret as in the originally
reported case. This confusing and error-prone behavior needs to be revised
separately.

GitOrigin-RevId: 1f48fe0e0936fb2007022dfc889c44ad32e1eaf0
2020-06-18 19:23:01 +03:00
Mikhail Golubev
16eebe034e PY-25001 Implement Complete Current Statement for collection literals
It works by automatically inserting a trailing comma before a line break in
multiline collection literals. Additionally, a colon is inserted between a key
and a value in dict literals.

Because of the language ambiguity regarding syntax of some incomplete collection
literals, colon is not inserted after the first key of a dict literal
(it's indistinguishable from a set literal with one item), and comma is not
inserted after the first item of a parenthesized tuple (it's indistinguishable
from a parenthesized expression).

The idea was taken from such implementation of Complete Current Statement for
JSON.

GitOrigin-RevId: 49ff9857d8c12476bfa9aacaed0b49faa99810fd
2020-06-10 22:57:53 +03:00
andrey.matveev
de6a66846e [pycharm] Delete testNumberOfOccurrencesNamedArgsWithPrefix test
GitOrigin-RevId: 4d1a8134438a09f9388f6cb8aae2513fc4d73d75
2020-06-05 13:08:31 +03:00
andrey.matveev
bf9aa197ad PY-41056 Impl new ml completion features
GitOrigin-RevId: 2c7d1296854ced8f291aa7ee399ebd1881e2b005
2020-04-15 10:02:07 +00:00
andrey.matveev
073af04a6a PY-38581 Implement previous calls feature
GitOrigin-RevId: 5682350bfd95a6abfc325b511c55d6e27b41476a
2020-02-25 05:37:15 +00:00
andrey.matveev
c4950335ac PY-39607 Add new features for ml completion
GitOrigin-RevId: caf60bd52a1872a2eb53f3617a6ffb51b195ff33
2020-01-29 13:41:50 +00:00
andrey.matveev
92e41799f2 PY-37720 Impl python ml completion features
GitOrigin-RevId: 3b8029ab460672b286d7b1cf96fd35bf9c57bf59
2019-10-11 08:01:46 +00:00
aleksei.kniazev
16786f4b89 added inspection for deleted variables with no qualifiers (PY-4537)
- treat deletion by slice and subscription expressions as read access
- delete instruction in dataflow only inserted for PyReferenceExpression

GitOrigin-RevId: 72ae5e964a1e287e007dabb8f41433b284523a02
2019-10-02 13:08:48 +00:00
Aleksei Kniazev
3c203f5382 IDEA-CR-51590: added self parameter for methods on smart enter (PY-35163)
self is inserted for methods that are not class-/staticmethod
cls is inserted for classmethod
empty parameter list for staticmethod

GitOrigin-RevId: 9743ae50c89c069b8123b8b6eb26fb841dd1f5b2
2019-09-04 15:08:25 +00:00
Semyon Proshev
a86c0b2bbe Fix building CFG for assignment expressions so target is visited after assigned value (PEP 572) (PY-33886)
GitOrigin-RevId: ac57729f8a9188a9fccf12936675c5818969435c
2019-07-02 06:52:16 +03:00
aleksei.kniazev
9034bb6261 ignore ifs in comprehensions when determine if the file is runnable 2019-01-29 16:07:46 +03:00
aleksei.kniazev
89d9f69135 corrected name check for run icon 2019-01-28 19:02:19 +03:00
Semyon Proshev
63b4650c38 Create pending edge from continue when loop has at least one iteration (PY-29767)
There is no edge between loop statement and next after loop instruction
when loop has at least one iteration
so `continue` is marked as one more last instruction in the loop
2018-11-09 19:21:07 +03:00
Elizaveta Shashkova
1ac30ed12d Fix unbound local variable after exception inside with statement (PY-13919)
Change control flow to make it more correct for with statement. Handle special case for Unbound local variable inspection and add tests
2018-05-29 19:54:26 +03:00
Semyon Proshev
a6c795551c Don't collect imported with as elements as named elements (PY-21837)
Because such elements are not presented in stub.
2018-04-09 17:51:35 +03:00
Semyon Proshev
42e4964191 Visit else in while in any case even if condition is True (PY-28972)
But when condition is `True`, edge to `else` is not created.
2018-03-26 19:36:15 +03:00
Semyon Proshev
6933c9d9e0 Update CFG to honour condition values in if statements (PY-24750) 2018-03-13 16:08:46 +03:00
Semyon Proshev
ddd6156be0 Consider elif inner instructions as branching points (PY-23003) 2018-03-12 17:22:16 +03:00
Semyon Proshev
222cb0d70e Don't use resolve while evaluating expressions for CFG 2018-03-07 19:21:54 +03:00
Semyon Proshev
fbee38877a Fix processing normal exits from try-except when they goes out of scope (PY-22184) 2018-02-20 15:21:44 +03:00
Semyon Proshev
1b0e13c8b0 More precise CFG for for cycles by using updated evaluator (PY-19856) 2018-02-20 13:56:13 +03:00
Semyon Proshev
494a53a364 Merge PyConstantExpressionEvaluator and PyEvaluator (PY-19856) 2018-02-20 13:56:10 +03:00
Mikhail Golubev
cbb5fe587a PY-27604 Exclude instantiated metaclasses from ancestor types of a class
but properly take them into account in PyClassType#getMetaClassType().

This check was implemented there instead of PyClass#getMetaClassType()
itself, since the former method already looks up for explicitly declared
metaclasses in ancestors unlike PyClassImpl that considers only those
specified directly in the class definition or its containing module.

Moreover, there are actual usages that expects such behavior from
PyClass#getMetaClassType(), e.g. PyAbstractClassInspection.
2018-01-11 12:45:08 +03:00
Mikhail Golubev
f7da96db03 PY-27656 PY-27604 Exclude ancestors of metaclasses from class hierarchy
in case this relationship was created implicitly by inheriting an
instance of metaclass.

It also fixes warnings about the first parameter of SQLAlchemy model
methods being named "self" instead of "cls" since we no longer consider
these classes descendants of "type".
2018-01-11 12:45:08 +03:00
Semyon Proshev
6b2814e09c Add forgotten test data (PY-25974) 2017-12-25 14:55:17 +03:00
Semyon Proshev
85bea17b09 Add pending edge from left operand in boolean expressions (PY-25974) 2017-12-25 14:24:04 +03:00
Semyon Proshev
2c1c972962 Support positive iterations in control flow (PY-14840, PY-22003) 2017-12-11 15:43:05 +03:00
Semyon Proshev
8d3d94ee88 PY-20889 Fixed: Unresolved attribute after isinstance check in boolean expression
Consider `and` and `or` expressions in PyControlFlowBuilder and use PyTypeAssertionEvaluator to make assertion for right expression.
2017-09-29 19:12:46 +03:00
Mikhail Golubev
247e0aeeb1 PY-21175 Add write instructions for assignments to attributes in CFG
to handle, in particular, lazy initialization scenarios

For instance, the statement "foo.bar = 42" should add a node
"WRITE ACCESS foo.bar" containing the corresponding target expression.

Additionally, "Unused local" inspection doesn't consider writes to
attributes, since, strictly speaking, these are not local names.
2017-07-24 14:38:41 +03:00
Mikhail Golubev
bf22c95cc3 PY-21175 Add implicit negative type assertion after "if" in CFG unconditionally
The check that we should add such synthetic node only if there were no
pending edges inside the body of if statement (e.g. if it contained only
break/continue/return for the enclosing loop) seems doubtful and
doesn't cover the simplest/most common cases.
2017-07-24 14:38:41 +03:00
Mikhail Golubev
1d3eda1e2d PY-22094 Include assert statements in CFG to resolve names in f-strings inside
Otherwise it's impossible to flow-sensitively resolve names referenced
inside such f-strings since we use the containing statement as the anchor
node in the graph.
2017-03-28 16:44:21 +02:00
Andrey Vlasovskikh
8373787e76 Consider class foo(six.with_metaclass(bar)) as new-style class (PY-22806)
Even if there are no new-style parents, the fact of having a metaclass
defined in a class is sufficient to treat it as a new-style class.
2017-03-10 21:08:00 +03:00