92 Commits

Author SHA1 Message Date
Mikhail Golubev
1da22d34fd PY-54560 Support PEP-681 dataclass_transform
`dataclass_transform` support posed a number of challenges to the current
AST/PSI stubs separation in our architecture. For the standard "dataclasses"
module and the "attrs" package API, we could rely on well-known names
and defaults to recognize and preserve the information about decorator
arguments and field specifier arguments in PSI stubs. With `dataclass_transform`
however, effectively any decorator call or extending any base class can indicate
using a "magical" API that should generate dataclass-like entities.
At the moment of building PSI stubs we can't be sure, because we can't leave
the boundaries of the current file to resolve these names and determine if these
decorators and base classes are decorated themselves with `dataclass_transform`.

To support that, we instead rely on well-known keyword argument names documented
in the spec, e.g. "kw_only" or "frozen". In other words, whenever we encounter
any decorator call or a superclass list with such keyword arguments, we generate
a `PyDataclassStub` stub for the corresponding class.

The same thing is happening with class attribute initializers, i.e. whenever
we see a function call with argument such as "default" or "kw_only" in their
RHS, we generate a `PyDataclassFieldStub` for the corresponding target expression.
Both of these stub interfaces now can contain null values for the corresponding
properties if they were not specified directly in the class definition.

Finally, for the `dataclass_transform` decorator itself, a new custom decorator stub
was introduced -- `PyDataclassTransformDecoratorStub`, it preserves its keyword
arguments, such as "keyword_only_default" or "frozen_default" controlling
the default properties of generated dataclasses.

Later, when we need concluded information about specific dataclass properties,
e.g. in `PyDataclassTypeProvider` to generate a constructor signature, or in
`PyDataclassInspection`, we try to "resolve" this incomplete information from stubs
into finalized `PyDataclassParameters` and `PyDataclassFieldParameters` that
contain non-null versions of the same fields. The main entry points for that
are `resolveDataclassParameters` and `resolveDataclassFieldParameters`.
These methods additionally handle the situations where decorators, superclass
lists and field specifiers lack any keyword arguments, and thus, there were no
automatically created custom stubs for them.

All the existing usages of `PyDataclassStub` and `PyDataclassFieldStub`
were updated to operate on `PyDataclassParameters` and `PyDataclassFieldParameters`
instead.

Counterparts of the tests on various inspection checks for the standard dataclasses
definitions were added for dataclasses created with `dataclass_transform`, even
though the spec is unclear on some aspects the expected type checker semantics, e.g.
if combining "eq=False" and "order=True" or specifying both "default" and
"default_factory" for a field should be reported.
I tried to follow common sense when enabling existing checks for such arbitrary
user-defined dataclass APIs.

GitOrigin-RevId: 4180a1e32b5e4025fc4e3ed49bb8d67af0d60e66
2024-09-09 11:34:15 +00:00
Daniil Kalinin
7751fceaed PY-71002 PEP-696: Support new syntax for default types of Type Parameters in new-style declarations
- PEP-696 adds a new syntax for declaring the default types of Type Parameters in new-new style generic classes, functions and type alias statements. Support these grammar changes.
- Store info about default types in stubs for Type Parameters
- Increment the stub version counter in PyFileElementType

GitOrigin-RevId: b6b22e3eaa86ce06132885781e5775a89bf4b840
2024-09-07 11:11:12 +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
0020fdc7e9 PY-34617 Take into account sys.version_info checks when analyzing Python files
Added test for version-specific stub elements

GitOrigin-RevId: fde82213359e577cb45006d8a795ec8e44129328
2024-08-13 20:39:16 +00:00
Petr
a3608ebd22 PY-34617 Remove if stubs
GitOrigin-RevId: 13f49394e92daca80af9c82ac9ac1a439a6ae1f5
2024-08-12 09:37:32 +00:00
Petr
93b9066edf PY-34617 Support version check
GitOrigin-RevId: 3318ff79cdcc5ba0ce5e4feb65abad5ad0f4acfa
2024-07-28 00:24:15 +00:00
Andrey Vokin
673383c3da PY-61651 Deprecation highlighting with PEP 702 @deprecated decorator
GitOrigin-RevId: 426e7001d20849d7029fea55431d3e2cfae3eb11
2024-06-10 15:58:53 +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
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
Daniil Kalinin
1c5b0c5ac3 PY-62608 PEP 695 Type Parameter Syntax: PSI stubs for type parameter lists and type aliases
GitOrigin-RevId: b8707b4677519bab5bcf922c9747eb8388e3157b
2023-09-28 15:05:30 +00:00
Irina Fediaeva
7379dc43c5 PY-49946: Support 'kw_only' in @dataclass args and dataclasses.field() args
GitOrigin-RevId: fe6f32a2cb8fa65b122968e1e9cf9d2126f92398
2023-04-12 17:59:46 +00:00
Mikhail Golubev
84c48c48a9 PY-47532 Support new API and namespace of "attrs" package
GitOrigin-RevId: a8a0f909b21cc9f3b95a7b823452599374a943a9
2022-08-18 16:13:12 +00:00
Semyon Proshev
049680d7cf Remove dataclasses.py from test data after enabling corresponding pyi stub from typeshed
GitOrigin-RevId: fbb411d4ee07c3119e9c03892f5b3001d9e44a20
2021-08-19 17:20:30 +00:00
Semyon Proshev
56a1c00c2a Make python-latest project descriptor default in tests
Update test data and reduce manual language level setup

GitOrigin-RevId: d6ddec1b2b29a2b7f1a34cfb5982fd975de78d65
2021-03-15 17:33:04 +00:00
Mikhail Golubev
bb31fdc67d PY-42334 Add support for PEP 613 typing.TypeAlias
Both typing.TypeAlias (available only in 3.10) and typing_extensions.TypeAlias
names are supported.

RHS values of assignments annotated with TypeAlias are always retained in
stubs and injected into (if it's a string literal), regardless of whether
they look similar to a well-formed type hint. It seems natural to assume
that if a user employs such as specific marker as "TypeAlias" at all, they
clearly indicate that the value is supposed to be a type.

The inspections "Type hints definitions and usages" and "Final classes, methods
and variables" properly analyse RHS of assignments annotated with TypeAlias.
Type hinting inspection also reports illegal usages of TypeAlias, as it was
done for other special forms in the typing module.

The type of such variables themselves is Any, however they're still displayed
as having the type "TypeAlias" in Quick Documentation to avoid confusion.

GitOrigin-RevId: fab02f6e1060c0994e1d21201768e7b28ba7d9e0
2020-10-19 19:46:54 +00:00
Mikhail Golubev
a717a7c145 PY-41305 Update PyDecorator API and docs for PEP 614
- Added PyDecorator.getExpression() to reflect that in Python 3.9
an arbitrary expression can be used after "@". Reused it where possible
in the implementation.
- Removed hasPlainReferenceCallee() in favor of existing getQualifiedName().
Updated javadoc for the latter to clarify how it handles non-trivial callees.
- Added PSI stub tests to make sure that right values are still persisted
with the new flexible decorator grammar.

GitOrigin-RevId: 8109b834c8d257c72f6f6e3c0ce5005060a2b971
2020-06-20 17:29:44 +03:00
Semyon Proshev
327f9cbffd Support kw_only in attr.ib (PY-33189)
GitOrigin-RevId: 7354162dc40909842f95f9eb5c9e6b507c544ac8
2020-06-16 02:00:57 +03:00
Semyon Proshev
1efa876ad0 Support kw_only in attr.s (PY-34374)
GitOrigin-RevId: a8e78103f3373c967cd429b21e0ced0d530b468d
2019-12-04 12:33:00 +00:00
Lada Gagina
0f51507660 Add tests and fixes to TypedDict support
PY-38422 Unify warning message for 'clear' and 'popitem' TypedDict methods
PY-38505 Infer proper type for TypedDict subscription expressions
PY-38439 Fix TypedDict consistency check
PY-38415 Fix TypedDict keys order in parameter info
PY-38413 Add * before all arguments in TypedDict parameter hint
PY-38873 Fix detection of TypedDict subscription expression type when value is a dict, Dict or TypedDict

GitOrigin-RevId: 25ff441042f2e2d7791e28632a5016fb367685df
2019-11-22 21:03:15 +00:00
Semyon Proshev
848405926a Support specifying namedtuple name and fields through keyword arguments in any order (PY-29983, PY-34134)
GitOrigin-RevId: 8489e8af74b80dd3fd700a2039d97d2de985ce3b
2019-09-18 15:22:30 +00:00
Semyon Proshev
ebeaf3cb98 Support specifying namedtuple fields through keyword argument (PY-29983, PY-34134)
GitOrigin-RevId: bd3482d76bab385247df02230403ef5f721fa2db
2019-09-16 17:03:01 +00:00
Semyon Proshev
b78c1750d6 Don't exclude assignment expressions in top-level comprehensions from stubbing as they are put to file scope (PEP 572) (PY-33886)
GitOrigin-RevId: cc3466aeec9d4cedb77844584712970e5d43a859
2019-07-02 06:52:16 +03:00
Rustam Vishnyakov
123242c4b2 EditorConfig documentation test
GitOrigin-RevId: fd52ace3d7a32ecd02c2c5ab90e077967604c15e
2019-06-16 04:03:21 +03:00
Egor Zhdan
c1813cfb52 Cleanup: NotNull/Nullable
GitOrigin-RevId: b8e892f32ea84c2115973155dba7127b892cc36e
2019-06-16 04:02:08 +03:00
Eldar Abusalimov
18962d3d42 Merge remote-tracking branch 'origin/master' into eldar/cidr-debugger
# Conflicts:
#	CIDR/cidr-debugger/resources/META-INF/CidrDebuggerPlugin.xml

GitOrigin-RevId: de8e0298bef122544fd1af40c209050903b3d263
2019-06-16 03:02:17 +03:00
Eldar Abusalimov
1120c30a56 Merge remote-tracking branch 'origin/master' into eldar/cidr-debugger
# Conflicts:
#	CIDR/clion/src/com/jetbrains/cidr/cpp/toolchains/MSVC.java

GitOrigin-RevId: f3593b526d1870f32b3f1451cab0c6a653e5beb5
2019-06-16 03:01:38 +03:00
Anna Kozlova
91f7445298 constructor reference: don't ignore constructor parameters during method reference inference (IDEA-185578)
GitOrigin-RevId: e836468e05db28157713e9edd3c70382f8ecdebc
2019-06-13 03:15:49 +03:00
Egor Zhdan
39d2d77155 Cleanup: NotNull/Nullable
GitOrigin-RevId: b8e892f32ea84c2115973155dba7127b892cc36e
2019-06-13 03:14:35 +03:00
Julia Beliaeva
3fee046d20 [vcs-log] cleanup RootCellRenderer
GitOrigin-RevId: bba216626193ac756cd133b8ae9bc4e577abea42
2019-06-10 04:10:14 +03:00
Eldar Abusalimov
dcaeb8f8c7 OC-4061: ApplicationUtil: Make runWithCheckCanceled() return result
GitOrigin-RevId: 3aa9f23cc5097e644eddf82a4faca669759b4760
2019-06-10 04:09:35 +03:00
Semyon Proshev
a2230a6e6b Apply API notes planned for 2019.2
GitOrigin-RevId: 979edb0c2c360b27cbac2cf313fbca182dbfeba0
2019-06-08 15:09:07 +03:00
Semyon Proshev
cb33bf9cbd Stub for positional-only parameters delimiter (PEP 570) (PY-35512)
GitOrigin-RevId: ca971a1e3ed0880fc818527da216b186a85cd62a
2019-06-04 19:12:21 +03:00
Semyon Proshev
54898c22c6 Inline __slots__ invalidation logic into PyClass (PY-29268) 2018-05-29 17:24:39 +03:00
Semyon Proshev
4c281d2158 Update stubs tests for dataclasses and attrs fields (PY-26354, PY-27398) 2018-05-29 16:50:05 +03:00
Semyon Proshev
29f70e9815 Sync dataclass parsing and testdata with March 04, 2018 changes in PEP-557 (PY-28957) 2018-03-20 14:20:23 +03:00
Mikhail Golubev
9bbbbc6eab PY-21191 PY-28879 Support nested tuples in type hints inside comments
by reusing the logic from "Convert to variable annotation" intention

This change also partially remedies the problem described in PY-28879
when the whole file was parsed whenever we found typing.Tuple in a
variable type comment, because we no longer transform type hints like
"int, str" into "Tuple[int, str]" as an intermediate step and process
them right away instead. We still need to handle general type comments
with unpacking in stub-safe manner, though.
2018-03-13 12:04:33 +03:00
Elizaveta Shashkova
79cc4d9ef4 Support for typing.NewType (PY-21302) 2018-02-06 20:24:29 +03:00
Semyon Proshev
bdce6311ca Support dataclasses fields defined via field function (PY-27398)
Update PyDataclassesTypeProvider to ignore such fields or correctly specify default value for them.
Create custom target stubs for such fields to store parameters.
2018-01-22 22:52:08 +03:00
Semyon Proshev
cff0305fb8 Don't forget type declarations while collecting class attributes (PY-26163) 2018-01-22 21:41:16 +03:00
Semyon Proshev
49d3aade75 Enable pyi-stubs for collections module (PY-23259, PY-21415, PY-17865, PY-17206)
Infer `tuple` class type when `collections.namedtuple` could not be analyzed.
Infer `namedtuple` class type when `typing.NamedTuple` could not be analyzed.
Create callable type for `typing.Callable`.
Update ignoring `__getitem__` for ancestors, docstrings and annotations.
Infer superclass collection type correctly.
2018-01-19 18:34:31 +03:00
fitermay
6d9cd9f45c PY-25655: Store super classes text in stub 2018-01-12 15:35:46 +03:00
Mikhail Golubev
ef615cb4a4 PY-24969 Don't keep PyAnnotationStubs for assignments and type declarations
Namely, retain them only for function return type annotations and
named parameters, since for these two PSI nodes we use
getStubOrPsiChild() in getAnnotation() as a way to quickly check
whether there is annotation at all. Otherwise, stubs for annotations
of local variables might be stored directly in a function stub and
thus falsely recognized as a type hint for its return value.
2017-08-31 14:29:35 +03:00
Mikhail Golubev
ad89420209 PY-18816 Test that accessing annotation in type declarations doesn't cause unstubbing 2017-07-19 19:28:31 +03:00
Mikhail Golubev
2cc6c4daf0 PY-18816 Resolve generic classes and type parameters using stubs 2017-07-19 19:28:31 +03:00
Mikhail Golubev
80fee7c7bb PY-18816 Store literal text of subscripted base classes in PyClassStub 2017-07-19 19:28:31 +03:00
Mikhail Golubev
1f48d16790 PY-18816 Add a test on unstubbing due to unresolved references in annotations 2017-07-19 19:28:31 +03:00
Mikhail Golubev
840769d43f PY-18816 Test that glued and long string literals are not saved in stubs 2017-07-19 19:28:31 +03:00
Mikhail Golubev
f20ab38e84 PY-18816 Exclude qualified references from type aliases stubs
Since, otherwise, these custom stubs conflict with the standard
"initializer" field of PyTargetExpressionStub and, thus, break
resolve in stubbed files, e.g. when exported symbols are aliased in
"__init__.py" of a package, etc. The current workaround is not to
keep RHS text of such assignments in the custom stubs, relying
on existing functionality of PyTypingAliasStubType instead, but inspect
both when extracting type aliases from the stub tree.
2017-07-19 19:28:31 +03:00
Mikhail Golubev
a701318d1b PY-18816 Store RHS text of assignments looking like type aliases in stubs 2017-07-19 19:28:31 +03:00