forked from VimPlug/jedi
Compare commits
251 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b80c0b8992 | ||
|
|
1b33f0d77c | ||
|
|
3454ebb1de | ||
|
|
3d2ce2e01f | ||
|
|
88d3da4ef6 | ||
|
|
15a7513fd0 | ||
|
|
0f35a1b18b | ||
|
|
4ea7981680 | ||
|
|
3a436df7ac | ||
|
|
c1e9aee15b | ||
|
|
6e5f201f6c | ||
|
|
356923e40d | ||
|
|
86c3a02c8c | ||
|
|
f4ca099afb | ||
|
|
d411290dff | ||
|
|
7c27da8d68 | ||
|
|
13063221f2 | ||
|
|
e83228478e | ||
|
|
e5a72695a8 | ||
|
|
4238198eea | ||
|
|
a10b158bcc | ||
|
|
503c88d987 | ||
|
|
d53a8ef81c | ||
|
|
eb80dc08f3 | ||
|
|
5f4afa27e5 | ||
|
|
e49032ed6b | ||
|
|
e20c3c955f | ||
|
|
a3fd90d734 | ||
|
|
999332ef77 | ||
|
|
e140523211 | ||
|
|
bd1edfce78 | ||
|
|
7dcb944b05 | ||
|
|
50778c390f | ||
|
|
e0797be681 | ||
|
|
8912a35502 | ||
|
|
77cf382a1b | ||
|
|
70efe2134c | ||
|
|
472ee75e3c | ||
|
|
68c7bf35ce | ||
|
|
efc7248175 | ||
|
|
c4f0538930 | ||
|
|
35a12fab7a | ||
|
|
a856a93bd1 | ||
|
|
60f0894f66 | ||
|
|
699c930bd4 | ||
|
|
9dd76c7ce5 | ||
|
|
74b46f3ee3 | ||
|
|
027e29ec50 | ||
|
|
f9beef0f6b | ||
|
|
d866ec0f80 | ||
|
|
6aee460b1d | ||
|
|
0315e6ee8f | ||
|
|
ce109a8cdf | ||
|
|
ecb922c6ff | ||
|
|
41e9e957e7 | ||
|
|
b225678a42 | ||
|
|
30adf43a89 | ||
|
|
be6df62434 | ||
|
|
e53359ad88 | ||
|
|
6e5d5b779c | ||
|
|
91ffdead32 | ||
|
|
2859e4f409 | ||
|
|
8ee4c26ae4 | ||
|
|
4d09ac07e4 | ||
|
|
82d1902f38 | ||
|
|
857c9be500 | ||
|
|
e839683e91 | ||
|
|
255186376e | ||
|
|
a67deeb602 | ||
|
|
d543d1d004 | ||
|
|
9d18b7c36d | ||
|
|
340dedd021 | ||
|
|
fff6e0ce2e | ||
|
|
473b35e6ec | ||
|
|
a0527a5af5 | ||
|
|
bbbaad21e8 | ||
|
|
ee90cd97b6 | ||
|
|
68e435cc66 | ||
|
|
b69d4d87c3 | ||
|
|
0fcb4468e7 | ||
|
|
5c578e1899 | ||
|
|
9bad42c0db | ||
|
|
3118462a93 | ||
|
|
065580b5d4 | ||
|
|
39c8317922 | ||
|
|
ab97e9f784 | ||
|
|
f7c9ee9433 | ||
|
|
8792c6d432 | ||
|
|
a4574a50d0 | ||
|
|
f11014fc5d | ||
|
|
54a6dadde3 | ||
|
|
740b474eda | ||
|
|
950ce70239 | ||
|
|
6982a49977 | ||
|
|
9b8cece7ef | ||
|
|
162034b387 | ||
|
|
7494c9495e | ||
|
|
7d77f61040 | ||
|
|
11280ef502 | ||
|
|
94ec4b873a | ||
|
|
f8e502f90c | ||
|
|
dc20f2e5a0 | ||
|
|
4e175ca82b | ||
|
|
30e9b7b5be | ||
|
|
83545bc9ec | ||
|
|
57e7b83455 | ||
|
|
0770372857 | ||
|
|
7e533ca7e1 | ||
|
|
a60fdba1d4 | ||
|
|
9d399a9229 | ||
|
|
770cdade00 | ||
|
|
29890c1f29 | ||
|
|
159566e1a0 | ||
|
|
a80618a2df | ||
|
|
4bc1b6ef99 | ||
|
|
d655d65d3a | ||
|
|
51f4a99a1e | ||
|
|
93c14d2e6e | ||
|
|
57aefed6ea | ||
|
|
8a4b079d0f | ||
|
|
62cbcb0844 | ||
|
|
d8420d0f72 | ||
|
|
886279fb6d | ||
|
|
ff3a7f367f | ||
|
|
1f70e3301e | ||
|
|
a34c348a55 | ||
|
|
972123c9c9 | ||
|
|
6455a14841 | ||
|
|
8d9e3ab3a7 | ||
|
|
048173e467 | ||
|
|
1947e7dd56 | ||
|
|
01d8da8f73 | ||
|
|
6ea5ad7b19 | ||
|
|
cd4ca74d7a | ||
|
|
67d6262f45 | ||
|
|
5f19237a3e | ||
|
|
f2444b4be5 | ||
|
|
7028bbb5d5 | ||
|
|
3699ba0aa7 | ||
|
|
72d34f3d7d | ||
|
|
a28bd24bef | ||
|
|
54cb64292c | ||
|
|
d421b920fa | ||
|
|
c137eb6918 | ||
|
|
d67facc922 | ||
|
|
7023b645b1 | ||
|
|
b5120cc90b | ||
|
|
483e78993d | ||
|
|
3dbcd2c6de | ||
|
|
ca36fcfa4b | ||
|
|
825c6b93bf | ||
|
|
c22585c6f2 | ||
|
|
431d1e104d | ||
|
|
adcd6ade8b | ||
|
|
32a1dd33a6 | ||
|
|
9ea01bcc69 | ||
|
|
77cfefc1cc | ||
|
|
ff7d6c6e4c | ||
|
|
6ee33bd385 | ||
|
|
0fbc2aafa3 | ||
|
|
fe7e350051 | ||
|
|
b814ca2951 | ||
|
|
aae2a8e3ed | ||
|
|
67e0bec597 | ||
|
|
c71e06fcb3 | ||
|
|
bbd5bcf3ca | ||
|
|
d888c1b266 | ||
|
|
83d0e23800 | ||
|
|
dc4e48d7c7 | ||
|
|
664b10a5c6 | ||
|
|
36a4b7d48c | ||
|
|
b0025ee6ba | ||
|
|
fac0b7f068 | ||
|
|
aeadba7cad | ||
|
|
fd0e6aed96 | ||
|
|
c89fa8e927 | ||
|
|
00e23ddcee | ||
|
|
66e97e5b93 | ||
|
|
0f5ea3de5f | ||
|
|
e47bbbb851 | ||
|
|
eaab706038 | ||
|
|
41455480be | ||
|
|
0a670d10dd | ||
|
|
6b73d5c1bf | ||
|
|
a3fed3b6a6 | ||
|
|
66c52b4bc7 | ||
|
|
89f9a3a7f1 | ||
|
|
3a30008cc4 | ||
|
|
b0d5fc2bd0 | ||
|
|
6e5db3f479 | ||
|
|
85780111e0 | ||
|
|
0ba48bbb9d | ||
|
|
26f7878d97 | ||
|
|
8027e1b162 | ||
|
|
78a53bf005 | ||
|
|
8485df416d | ||
|
|
94e78340e1 | ||
|
|
f454989859 | ||
|
|
e779f23ac7 | ||
|
|
3c40363a39 | ||
|
|
a6cf2c338a | ||
|
|
2a7311c1a0 | ||
|
|
81427e4408 | ||
|
|
804e4b0ca2 | ||
|
|
3475ccfbd3 | ||
|
|
9723a0eed0 | ||
|
|
658f80fa1e | ||
|
|
31c2c508c3 | ||
|
|
6c9cab2f8e | ||
|
|
0a6ad1010c | ||
|
|
3a60943f6e | ||
|
|
4d1e00c3ab | ||
|
|
e15f51ecc1 | ||
|
|
eaa66b3dbb | ||
|
|
239d9e0b22 | ||
|
|
40e1e3f560 | ||
|
|
c243608ac6 | ||
|
|
e25750ecef | ||
|
|
1a306fddbf | ||
|
|
ec425ed2af | ||
|
|
fa1e9ce9a7 | ||
|
|
8447d7f3e4 | ||
|
|
27e13e4072 | ||
|
|
9fd4aab5da | ||
|
|
8b0d391ac1 | ||
|
|
fa0c064841 | ||
|
|
9e2089ef1e | ||
|
|
85c7f14562 | ||
|
|
695f0832b4 | ||
|
|
cfb7e300af | ||
|
|
f5faca014f | ||
|
|
7ff0d2d595 | ||
|
|
c28b337278 | ||
|
|
128695bd8e | ||
|
|
e194ab5951 | ||
|
|
c0ac341750 | ||
|
|
486695d479 | ||
|
|
8cb1b76ea4 | ||
|
|
e7755651a4 | ||
|
|
0c7384edc3 | ||
|
|
8f15f38949 | ||
|
|
96af7e4077 | ||
|
|
929fa9b452 | ||
|
|
08c5ab821f | ||
|
|
b6f761f13c | ||
|
|
72cf41f4c9 | ||
|
|
3602c10916 | ||
|
|
601bfb3493 | ||
|
|
021f081d8a | ||
|
|
54af6fa86d | ||
|
|
f193ae67e9 |
28
.github/workflows/ci.yml
vendored
28
.github/workflows/ci.yml
vendored
@@ -1,28 +1,30 @@
|
|||||||
name: ci
|
name: ci
|
||||||
on: [push, pull_request]
|
on: [push, pull_request, workflow_dispatch]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
tests:
|
tests:
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
os: [ubuntu-20.04, windows-2019]
|
os: [ubuntu-24.04, windows-2022]
|
||||||
python-version: ["3.10", "3.9", "3.8", "3.7", "3.6"]
|
python-version: ["3.13", "3.12", "3.11", "3.10", "3.9", "3.8"]
|
||||||
environment: ['3.8', '3.10', '3.9', '3.7', '3.6', 'interpreter']
|
environment: ['3.8', '3.13', '3.12', '3.11', '3.10', '3.9', 'interpreter']
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
submodules: recursive
|
||||||
|
|
||||||
- uses: actions/setup-python@v2
|
- uses: actions/setup-python@v5
|
||||||
if: ${{ matrix.environment != 'interpreter' }}
|
if: ${{ matrix.environment != 'interpreter' }}
|
||||||
with:
|
with:
|
||||||
python-version: ${{ matrix.environment }}
|
python-version: ${{ matrix.environment }}
|
||||||
|
allow-prereleases: true
|
||||||
|
|
||||||
- uses: actions/setup-python@v2
|
- uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: ${{ matrix.python-version }}
|
python-version: ${{ matrix.python-version }}
|
||||||
|
allow-prereleases: true
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: 'pip install .[testing]'
|
run: 'pip install .[testing]'
|
||||||
@@ -33,10 +35,10 @@ jobs:
|
|||||||
JEDI_TEST_ENVIRONMENT: ${{ matrix.environment }}
|
JEDI_TEST_ENVIRONMENT: ${{ matrix.environment }}
|
||||||
|
|
||||||
code-quality:
|
code-quality:
|
||||||
runs-on: ubuntu-20.04
|
runs-on: ubuntu-24.04
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
submodules: recursive
|
||||||
|
|
||||||
@@ -45,15 +47,15 @@ jobs:
|
|||||||
|
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: |
|
run: |
|
||||||
python -m flake8 jedi setup.py
|
python -m flake8 jedi test setup.py
|
||||||
python -m mypy jedi sith.py
|
python -m mypy jedi sith.py setup.py
|
||||||
|
|
||||||
coverage:
|
coverage:
|
||||||
runs-on: ubuntu-20.04
|
runs-on: ubuntu-24.04
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
submodules: recursive
|
||||||
|
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -14,3 +14,4 @@ record.json
|
|||||||
/.pytest_cache
|
/.pytest_cache
|
||||||
/.mypy_cache
|
/.mypy_cache
|
||||||
/venv/
|
/venv/
|
||||||
|
.nvimrc
|
||||||
|
|||||||
@@ -1,2 +1,21 @@
|
|||||||
|
version: 2
|
||||||
|
|
||||||
python:
|
python:
|
||||||
pip_install: true
|
install:
|
||||||
|
- method: pip
|
||||||
|
path: .
|
||||||
|
extra_requirements:
|
||||||
|
- docs
|
||||||
|
|
||||||
|
submodules:
|
||||||
|
include: all
|
||||||
|
|
||||||
|
sphinx:
|
||||||
|
configuration: docs/conf.py
|
||||||
|
|
||||||
|
build:
|
||||||
|
os: ubuntu-22.04
|
||||||
|
tools:
|
||||||
|
python: "3.11"
|
||||||
|
apt_packages:
|
||||||
|
- graphviz
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
Main Authors
|
Main Authors
|
||||||
------------
|
------------
|
||||||
|
|
||||||
- David Halter (@davidhalter) <davidhalter88@gmail.com>
|
- David Halter (@davidhalter) <davidhalter88@gmail.com>
|
||||||
@@ -62,6 +62,10 @@ Code Contributors
|
|||||||
- Andrii Kolomoiets (@muffinmad)
|
- Andrii Kolomoiets (@muffinmad)
|
||||||
- Leo Ryu (@Leo-Ryu)
|
- Leo Ryu (@Leo-Ryu)
|
||||||
- Joseph Birkner (@josephbirkner)
|
- Joseph Birkner (@josephbirkner)
|
||||||
|
- Márcio Mazza (@marciomazza)
|
||||||
|
- Martin Vielsmaier (@moser) <martin@vielsmaier.net>
|
||||||
|
- TingJia Wu (@WutingjiaX) <wutingjia@bytedance.com>
|
||||||
|
- Nguyễn Hồng Quân <ng.hong.quan@gmail.com>
|
||||||
|
|
||||||
And a few more "anonymous" contributors.
|
And a few more "anonymous" contributors.
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,35 @@ Changelog
|
|||||||
Unreleased
|
Unreleased
|
||||||
++++++++++
|
++++++++++
|
||||||
|
|
||||||
|
0.19.2 (2024-11-10)
|
||||||
|
+++++++++++++++++++
|
||||||
|
|
||||||
|
- Python 3.13 support
|
||||||
|
|
||||||
|
0.19.1 (2023-10-02)
|
||||||
|
+++++++++++++++++++
|
||||||
|
|
||||||
|
- Python 3.12 support (Thanks Peter!)
|
||||||
|
|
||||||
|
0.19.0 (2023-07-29)
|
||||||
|
+++++++++++++++++++
|
||||||
|
|
||||||
|
- Python 3.11 support
|
||||||
|
- Massive improvements in performance for ``Interpreter`` (e.g. IPython) users.
|
||||||
|
This especially affects ``pandas`` users with large datasets.
|
||||||
|
- Add ``jedi.settings.allow_unsafe_interpreter_executions`` to make it easier
|
||||||
|
for IPython users to avoid unsafe executions.
|
||||||
|
|
||||||
|
0.18.2 (2022-11-21)
|
||||||
|
+++++++++++++++++++
|
||||||
|
|
||||||
|
- Added dataclass-equivalent for attrs.define
|
||||||
|
- Find fixtures from Pytest entrypoints; Examples of pytest plugins installed
|
||||||
|
like this are pytest-django, pytest-sugar and Faker.
|
||||||
|
- Fixed Project.search, when a venv was involved, which is why for example
|
||||||
|
`:Pyimport django.db` did not work in some cases in jedi-vim.
|
||||||
|
- And many smaller bugfixes
|
||||||
|
|
||||||
0.18.1 (2021-11-17)
|
0.18.1 (2021-11-17)
|
||||||
+++++++++++++++++++
|
+++++++++++++++++++
|
||||||
|
|
||||||
|
|||||||
12
README.rst
12
README.rst
@@ -2,6 +2,9 @@
|
|||||||
Jedi - an awesome autocompletion, static analysis and refactoring library for Python
|
Jedi - an awesome autocompletion, static analysis and refactoring library for Python
|
||||||
####################################################################################
|
####################################################################################
|
||||||
|
|
||||||
|
**I released the successor to Jedi: A
|
||||||
|
Mypy-Compatible Python Language Server Built in Rust** - `ZubanLS <https://zubanls.com>`_
|
||||||
|
|
||||||
.. image:: http://isitmaintained.com/badge/open/davidhalter/jedi.svg
|
.. image:: http://isitmaintained.com/badge/open/davidhalter/jedi.svg
|
||||||
:target: https://github.com/davidhalter/jedi/issues
|
:target: https://github.com/davidhalter/jedi/issues
|
||||||
:alt: The percentage of open issues and pull requests
|
:alt: The percentage of open issues and pull requests
|
||||||
@@ -10,7 +13,7 @@ Jedi - an awesome autocompletion, static analysis and refactoring library for Py
|
|||||||
:target: https://github.com/davidhalter/jedi/issues
|
:target: https://github.com/davidhalter/jedi/issues
|
||||||
:alt: The resolution time is the median time an issue or pull request stays open.
|
:alt: The resolution time is the median time an issue or pull request stays open.
|
||||||
|
|
||||||
.. image:: https://github.com/davidhalter/jedi/workflows/ci/badge.svg?branch=master
|
.. image:: https://github.com/davidhalter/jedi/actions/workflows/ci.yml/badge.svg?branch=master
|
||||||
:target: https://github.com/davidhalter/jedi/actions
|
:target: https://github.com/davidhalter/jedi/actions
|
||||||
:alt: Tests
|
:alt: Tests
|
||||||
|
|
||||||
@@ -42,7 +45,7 @@ Jedi can currently be used with the following editors/projects:
|
|||||||
- `GNOME Builder`_ (with support for GObject Introspection)
|
- `GNOME Builder`_ (with support for GObject Introspection)
|
||||||
- Gedit (gedi_)
|
- Gedit (gedi_)
|
||||||
- wdb_ - Web Debugger
|
- wdb_ - Web Debugger
|
||||||
- `Eric IDE`_ (Available as a plugin)
|
- `Eric IDE`_
|
||||||
- `IPython 6.0.0+ <https://ipython.readthedocs.io/en/stable/whatsnew/version6.html>`_
|
- `IPython 6.0.0+ <https://ipython.readthedocs.io/en/stable/whatsnew/version6.html>`_
|
||||||
- `xonsh shell <https://xon.sh/contents.html>`_ has `jedi extension <https://xon.sh/xontribs.html#jedi>`_
|
- `xonsh shell <https://xon.sh/contents.html>`_ has `jedi extension <https://xon.sh/xontribs.html#jedi>`_
|
||||||
|
|
||||||
@@ -51,7 +54,8 @@ and many more!
|
|||||||
There are a few language servers that use Jedi:
|
There are a few language servers that use Jedi:
|
||||||
|
|
||||||
- `jedi-language-server <https://github.com/pappasam/jedi-language-server>`_
|
- `jedi-language-server <https://github.com/pappasam/jedi-language-server>`_
|
||||||
- `python-language-server <https://github.com/palantir/python-language-server>`_
|
- `python-language-server <https://github.com/palantir/python-language-server>`_ (currently unmaintained)
|
||||||
|
- `python-lsp-server <https://github.com/python-lsp/python-lsp-server>`_ (fork from python-language-server)
|
||||||
- `anakin-language-server <https://github.com/muffinmad/anakin-language-server>`_
|
- `anakin-language-server <https://github.com/muffinmad/anakin-language-server>`_
|
||||||
|
|
||||||
Here are some pictures taken from jedi-vim_:
|
Here are some pictures taken from jedi-vim_:
|
||||||
@@ -98,7 +102,7 @@ Features and Limitations
|
|||||||
Jedi's features are listed here:
|
Jedi's features are listed here:
|
||||||
`Features <https://jedi.readthedocs.org/en/latest/docs/features.html>`_.
|
`Features <https://jedi.readthedocs.org/en/latest/docs/features.html>`_.
|
||||||
|
|
||||||
You can run Jedi on Python 3.6+ but it should also
|
You can run Jedi on Python 3.8+ but it should also
|
||||||
understand code that is older than those versions. Additionally you should be
|
understand code that is older than those versions. Additionally you should be
|
||||||
able to use `Virtualenvs <https://jedi.readthedocs.org/en/latest/docs/api.html#environments>`_
|
able to use `Virtualenvs <https://jedi.readthedocs.org/en/latest/docs/api.html#environments>`_
|
||||||
very well.
|
very well.
|
||||||
|
|||||||
9
SECURITY.md
Normal file
9
SECURITY.md
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# Security Policy
|
||||||
|
|
||||||
|
If security issues arise, we will try to fix those as soon as possible.
|
||||||
|
|
||||||
|
Due to Jedi's nature, Security Issues will probably be extremely rare, but we will of course treat them seriously.
|
||||||
|
|
||||||
|
## Reporting Security Problems
|
||||||
|
|
||||||
|
If you need to report a security vulnerability, please send an email to davidhalter88@gmail.com. Typically, I will respond in the next few business days.
|
||||||
@@ -156,6 +156,14 @@ def jedi_path():
|
|||||||
return os.path.dirname(__file__)
|
return os.path.dirname(__file__)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def skip_pre_python311(environment):
|
||||||
|
if environment.version_info < (3, 11):
|
||||||
|
# This if is just needed to avoid that tests ever skip way more than
|
||||||
|
# they should for all Python versions.
|
||||||
|
pytest.skip()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def skip_pre_python38(environment):
|
def skip_pre_python38(environment):
|
||||||
if environment.version_info < (3, 8):
|
if environment.version_info < (3, 8):
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ to write my own version of a completion engine.
|
|||||||
The first idea was to execute non-dangerous code. But I soon realized, that
|
The first idea was to execute non-dangerous code. But I soon realized, that
|
||||||
this would not work. So I started to build a static analysis tool.
|
this would not work. So I started to build a static analysis tool.
|
||||||
The biggest problem that I had at the time was that I did not know a thing
|
The biggest problem that I had at the time was that I did not know a thing
|
||||||
about parsers.I did not did not even know the word static analysis. It turns
|
about parsers. I did not even know the word static analysis. It turns
|
||||||
out they are the foundation of a good static analysis tool. I of course did not
|
out they are the foundation of a good static analysis tool. I of course did not
|
||||||
know that and tried to write my own poor version of a parser that I ended up
|
know that and tried to write my own poor version of a parser that I ended up
|
||||||
throwing away two years later.
|
throwing away two years later.
|
||||||
@@ -53,7 +53,7 @@ quick and is pretty much feature complete.
|
|||||||
|
|
||||||
--------
|
--------
|
||||||
|
|
||||||
I will leave you with a small annectote that happend in 2012, if I remember
|
I will leave you with a small anecdote that happened in 2012, if I remember
|
||||||
correctly. After I explained Guido van Rossum, how some parts of my
|
correctly. After I explained Guido van Rossum, how some parts of my
|
||||||
auto-completion work, he said:
|
auto-completion work, he said:
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ Jedi's main API calls and features are:
|
|||||||
Basic Features
|
Basic Features
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
- Python 3.6+ support
|
- Python 3.8+ support
|
||||||
- Ignores syntax errors and wrong indentation
|
- Ignores syntax errors and wrong indentation
|
||||||
- Can deal with complex module / function / class structures
|
- Can deal with complex module / function / class structures
|
||||||
- Great ``virtualenv``/``venv`` support
|
- Great ``virtualenv``/``venv`` support
|
||||||
@@ -77,7 +77,7 @@ Performance Issues
|
|||||||
|
|
||||||
Importing ``numpy`` can be quite slow sometimes, as well as loading the
|
Importing ``numpy`` can be quite slow sometimes, as well as loading the
|
||||||
builtins the first time. If you want to speed things up, you could preload
|
builtins the first time. If you want to speed things up, you could preload
|
||||||
libriaries in |jedi|, with :func:`.preload_module`. However, once loaded, this
|
libraries in |jedi|, with :func:`.preload_module`. However, once loaded, this
|
||||||
should not be a problem anymore. The same is true for huge modules like
|
should not be a problem anymore. The same is true for huge modules like
|
||||||
``PySide``, ``wx``, ``tensorflow``, ``pandas``, etc.
|
``PySide``, ``wx``, ``tensorflow``, ``pandas``, etc.
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ using pip::
|
|||||||
|
|
||||||
If you want to install the current development version (master branch)::
|
If you want to install the current development version (master branch)::
|
||||||
|
|
||||||
sudo pip install -e git://github.com/davidhalter/jedi.git#egg=jedi
|
sudo pip install -e git+https://github.com/davidhalter/jedi.git#egg=jedi
|
||||||
|
|
||||||
|
|
||||||
System-wide installation via a package manager
|
System-wide installation via a package manager
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ Using Jedi
|
|||||||
==========
|
==========
|
||||||
|
|
||||||
|jedi| is can be used with a variety of :ref:`plugins <editor-plugins>`,
|
|jedi| is can be used with a variety of :ref:`plugins <editor-plugins>`,
|
||||||
`language servers <language-servers>` and other software.
|
:ref:`language servers <language-servers>` and other software.
|
||||||
It is also possible to use |jedi| in the :ref:`Python shell or with IPython
|
It is also possible to use |jedi| in the :ref:`Python shell or with IPython
|
||||||
<repl-completion>`.
|
<repl-completion>`.
|
||||||
|
|
||||||
@@ -16,7 +16,8 @@ Language Servers
|
|||||||
--------------
|
--------------
|
||||||
|
|
||||||
- `jedi-language-server <https://github.com/pappasam/jedi-language-server>`_
|
- `jedi-language-server <https://github.com/pappasam/jedi-language-server>`_
|
||||||
- `python-language-server <https://github.com/palantir/python-language-server>`_
|
- `python-language-server <https://github.com/palantir/python-language-server>`_ (currently unmaintained)
|
||||||
|
- `python-lsp-server <https://github.com/python-lsp/python-lsp-server>`_ (fork from python-language-server)
|
||||||
- `anakin-language-server <https://github.com/muffinmad/anakin-language-server>`_
|
- `anakin-language-server <https://github.com/muffinmad/anakin-language-server>`_
|
||||||
|
|
||||||
.. _editor-plugins:
|
.. _editor-plugins:
|
||||||
@@ -86,7 +87,7 @@ Gedit
|
|||||||
Eric IDE
|
Eric IDE
|
||||||
~~~~~~~~
|
~~~~~~~~
|
||||||
|
|
||||||
- `Eric IDE`_ (Available as a plugin)
|
- `Eric IDE`_
|
||||||
|
|
||||||
Web Debugger
|
Web Debugger
|
||||||
~~~~~~~~~~~~
|
~~~~~~~~~~~~
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ ad
|
|||||||
load
|
load
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__version__ = '0.18.1'
|
__version__ = '0.19.2'
|
||||||
|
|
||||||
from jedi.api import Script, Interpreter, set_debug_function, preload_module
|
from jedi.api import Script, Interpreter, set_debug_function, preload_module
|
||||||
from jedi import settings
|
from jedi import settings
|
||||||
|
|||||||
@@ -5,11 +5,24 @@ different Python versions.
|
|||||||
import errno
|
import errno
|
||||||
import sys
|
import sys
|
||||||
import pickle
|
import pickle
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
class Unpickler(pickle.Unpickler):
|
||||||
|
def find_class(self, module: str, name: str) -> Any:
|
||||||
|
# Python 3.13 moved pathlib implementation out of __init__.py as part of
|
||||||
|
# generalising its implementation. Ensure that we support loading
|
||||||
|
# pickles from 3.13 on older version of Python. Since 3.13 maintained a
|
||||||
|
# compatible API, pickles from older Python work natively on the newer
|
||||||
|
# version.
|
||||||
|
if module == 'pathlib._local':
|
||||||
|
module = 'pathlib'
|
||||||
|
return super().find_class(module, name)
|
||||||
|
|
||||||
|
|
||||||
def pickle_load(file):
|
def pickle_load(file):
|
||||||
try:
|
try:
|
||||||
return pickle.load(file)
|
return Unpickler(file).load()
|
||||||
# Python on Windows don't throw EOF errors for pipes. So reraise them with
|
# Python on Windows don't throw EOF errors for pipes. So reraise them with
|
||||||
# the correct type, which is caught upwards.
|
# the correct type, which is caught upwards.
|
||||||
except OSError:
|
except OSError:
|
||||||
|
|||||||
@@ -206,6 +206,7 @@ class Script:
|
|||||||
before magic methods and name mangled names that start with ``__``.
|
before magic methods and name mangled names that start with ``__``.
|
||||||
:rtype: list of :class:`.Completion`
|
:rtype: list of :class:`.Completion`
|
||||||
"""
|
"""
|
||||||
|
self._inference_state.reset_recursion_limitations()
|
||||||
with debug.increase_indent_cm('complete'):
|
with debug.increase_indent_cm('complete'):
|
||||||
completion = Completion(
|
completion = Completion(
|
||||||
self._inference_state, self._get_module_context(), self._code_lines,
|
self._inference_state, self._get_module_context(), self._code_lines,
|
||||||
@@ -230,6 +231,7 @@ class Script:
|
|||||||
:param prefer_stubs: Prefer stubs to Python objects for this method.
|
:param prefer_stubs: Prefer stubs to Python objects for this method.
|
||||||
:rtype: list of :class:`.Name`
|
:rtype: list of :class:`.Name`
|
||||||
"""
|
"""
|
||||||
|
self._inference_state.reset_recursion_limitations()
|
||||||
pos = line, column
|
pos = line, column
|
||||||
leaf = self._module_node.get_name_of_position(pos)
|
leaf = self._module_node.get_name_of_position(pos)
|
||||||
if leaf is None:
|
if leaf is None:
|
||||||
@@ -273,6 +275,7 @@ class Script:
|
|||||||
:param prefer_stubs: Prefer stubs to Python objects for this method.
|
:param prefer_stubs: Prefer stubs to Python objects for this method.
|
||||||
:rtype: list of :class:`.Name`
|
:rtype: list of :class:`.Name`
|
||||||
"""
|
"""
|
||||||
|
self._inference_state.reset_recursion_limitations()
|
||||||
tree_name = self._module_node.get_name_of_position((line, column))
|
tree_name = self._module_node.get_name_of_position((line, column))
|
||||||
if tree_name is None:
|
if tree_name is None:
|
||||||
# Without a name we really just want to jump to the result e.g.
|
# Without a name we really just want to jump to the result e.g.
|
||||||
@@ -365,10 +368,17 @@ class Script:
|
|||||||
|
|
||||||
:rtype: list of :class:`.Name`
|
:rtype: list of :class:`.Name`
|
||||||
"""
|
"""
|
||||||
|
self._inference_state.reset_recursion_limitations()
|
||||||
definitions = self.goto(line, column, follow_imports=True)
|
definitions = self.goto(line, column, follow_imports=True)
|
||||||
if definitions:
|
if definitions:
|
||||||
return definitions
|
return definitions
|
||||||
leaf = self._module_node.get_leaf_for_position((line, column))
|
leaf = self._module_node.get_leaf_for_position((line, column))
|
||||||
|
|
||||||
|
if leaf is not None and leaf.end_pos == (line, column) and leaf.type == 'newline':
|
||||||
|
next_ = leaf.get_next_leaf()
|
||||||
|
if next_ is not None and next_.start_pos == leaf.end_pos:
|
||||||
|
leaf = next_
|
||||||
|
|
||||||
if leaf is not None and leaf.type in ('keyword', 'operator', 'error_leaf'):
|
if leaf is not None and leaf.type in ('keyword', 'operator', 'error_leaf'):
|
||||||
def need_pydoc():
|
def need_pydoc():
|
||||||
if leaf.value in ('(', ')', '[', ']'):
|
if leaf.value in ('(', ')', '[', ']'):
|
||||||
@@ -400,6 +410,7 @@ class Script:
|
|||||||
the current module only.
|
the current module only.
|
||||||
:rtype: list of :class:`.Name`
|
:rtype: list of :class:`.Name`
|
||||||
"""
|
"""
|
||||||
|
self._inference_state.reset_recursion_limitations()
|
||||||
|
|
||||||
def _references(include_builtins=True, scope='project'):
|
def _references(include_builtins=True, scope='project'):
|
||||||
if scope not in ('project', 'file'):
|
if scope not in ('project', 'file'):
|
||||||
@@ -434,6 +445,7 @@ class Script:
|
|||||||
|
|
||||||
:rtype: list of :class:`.Signature`
|
:rtype: list of :class:`.Signature`
|
||||||
"""
|
"""
|
||||||
|
self._inference_state.reset_recursion_limitations()
|
||||||
pos = line, column
|
pos = line, column
|
||||||
call_details = helpers.get_signature_details(self._module_node, pos)
|
call_details = helpers.get_signature_details(self._module_node, pos)
|
||||||
if call_details is None:
|
if call_details is None:
|
||||||
@@ -553,6 +565,7 @@ class Script:
|
|||||||
return parso_to_jedi_errors(self._inference_state.grammar, self._module_node)
|
return parso_to_jedi_errors(self._inference_state.grammar, self._module_node)
|
||||||
|
|
||||||
def _names(self, all_scopes=False, definitions=True, references=False):
|
def _names(self, all_scopes=False, definitions=True, references=False):
|
||||||
|
self._inference_state.reset_recursion_limitations()
|
||||||
# Set line/column to a random position, because they don't matter.
|
# Set line/column to a random position, because they don't matter.
|
||||||
module_context = self._get_module_context()
|
module_context = self._get_module_context()
|
||||||
defs = [
|
defs = [
|
||||||
@@ -581,7 +594,7 @@ class Script:
|
|||||||
@validate_line_column
|
@validate_line_column
|
||||||
def extract_variable(self, line, column, *, new_name, until_line=None, until_column=None):
|
def extract_variable(self, line, column, *, new_name, until_line=None, until_column=None):
|
||||||
"""
|
"""
|
||||||
Moves an expression to a new statemenet.
|
Moves an expression to a new statement.
|
||||||
|
|
||||||
For example if you have the cursor on ``foo`` and provide a
|
For example if you have the cursor on ``foo`` and provide a
|
||||||
``new_name`` called ``bar``::
|
``new_name`` called ``bar``::
|
||||||
@@ -708,7 +721,6 @@ class Interpreter(Script):
|
|||||||
:param namespaces: A list of namespace dictionaries such as the one
|
:param namespaces: A list of namespace dictionaries such as the one
|
||||||
returned by :func:`globals` and :func:`locals`.
|
returned by :func:`globals` and :func:`locals`.
|
||||||
"""
|
"""
|
||||||
_allow_descriptor_getattr_default = True
|
|
||||||
|
|
||||||
def __init__(self, code, namespaces, *, project=None, **kwds):
|
def __init__(self, code, namespaces, *, project=None, **kwds):
|
||||||
try:
|
try:
|
||||||
@@ -729,7 +741,16 @@ class Interpreter(Script):
|
|||||||
super().__init__(code, environment=environment, project=project, **kwds)
|
super().__init__(code, environment=environment, project=project, **kwds)
|
||||||
|
|
||||||
self.namespaces = namespaces
|
self.namespaces = namespaces
|
||||||
self._inference_state.allow_descriptor_getattr = self._allow_descriptor_getattr_default
|
self._inference_state.allow_unsafe_executions = \
|
||||||
|
settings.allow_unsafe_interpreter_executions
|
||||||
|
# Dynamic params search is important when we work on functions that are
|
||||||
|
# called by other pieces of code. However for interpreter completions
|
||||||
|
# this is not important at all, because the current code is always new
|
||||||
|
# and will never be called by something.
|
||||||
|
# Also sometimes this logic goes a bit too far like in
|
||||||
|
# https://github.com/ipython/ipython/issues/13866, where it takes
|
||||||
|
# seconds to do a simple completion.
|
||||||
|
self._inference_state.do_dynamic_params_search = False
|
||||||
|
|
||||||
@cache.memoize_method
|
@cache.memoize_method
|
||||||
def _get_module_context(self):
|
def _get_module_context(self):
|
||||||
|
|||||||
@@ -105,8 +105,7 @@ class BaseName:
|
|||||||
# Compiled modules should not return a module path even if they
|
# Compiled modules should not return a module path even if they
|
||||||
# have one.
|
# have one.
|
||||||
path: Optional[Path] = self._get_module_context().py__file__()
|
path: Optional[Path] = self._get_module_context().py__file__()
|
||||||
if path is not None:
|
return path
|
||||||
return path
|
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
@@ -65,12 +65,15 @@ def _must_be_kwarg(signatures, positional_count, used_kwargs):
|
|||||||
return must_be_kwarg
|
return must_be_kwarg
|
||||||
|
|
||||||
|
|
||||||
def filter_names(inference_state, completion_names, stack, like_name, fuzzy, cached_name):
|
def filter_names(inference_state, completion_names, stack, like_name, fuzzy,
|
||||||
|
imported_names, cached_name):
|
||||||
comp_dct = set()
|
comp_dct = set()
|
||||||
if settings.case_insensitive_completion:
|
if settings.case_insensitive_completion:
|
||||||
like_name = like_name.lower()
|
like_name = like_name.lower()
|
||||||
for name in completion_names:
|
for name in completion_names:
|
||||||
string = name.string_name
|
string = name.string_name
|
||||||
|
if string in imported_names and string != like_name:
|
||||||
|
continue
|
||||||
if settings.case_insensitive_completion:
|
if settings.case_insensitive_completion:
|
||||||
string = string.lower()
|
string = string.lower()
|
||||||
if helpers.match(string, like_name, fuzzy=fuzzy):
|
if helpers.match(string, like_name, fuzzy=fuzzy):
|
||||||
@@ -138,6 +141,11 @@ class Completion:
|
|||||||
|
|
||||||
self._fuzzy = fuzzy
|
self._fuzzy = fuzzy
|
||||||
|
|
||||||
|
# Return list of completions in this order:
|
||||||
|
# - Beginning with what user is typing
|
||||||
|
# - Public (alphabet)
|
||||||
|
# - Private ("_xxx")
|
||||||
|
# - Dunder ("__xxx")
|
||||||
def complete(self):
|
def complete(self):
|
||||||
leaf = self._module_node.get_leaf_for_position(
|
leaf = self._module_node.get_leaf_for_position(
|
||||||
self._original_position,
|
self._original_position,
|
||||||
@@ -169,14 +177,19 @@ class Completion:
|
|||||||
|
|
||||||
cached_name, completion_names = self._complete_python(leaf)
|
cached_name, completion_names = self._complete_python(leaf)
|
||||||
|
|
||||||
|
imported_names = []
|
||||||
|
if leaf.parent is not None and leaf.parent.type in ['import_as_names', 'dotted_as_names']:
|
||||||
|
imported_names.extend(extract_imported_names(leaf.parent))
|
||||||
|
|
||||||
completions = list(filter_names(self._inference_state, completion_names,
|
completions = list(filter_names(self._inference_state, completion_names,
|
||||||
self.stack, self._like_name,
|
self.stack, self._like_name,
|
||||||
self._fuzzy, cached_name=cached_name))
|
self._fuzzy, imported_names, cached_name=cached_name))
|
||||||
|
|
||||||
return (
|
return (
|
||||||
# Removing duplicates mostly to remove False/True/None duplicates.
|
# Removing duplicates mostly to remove False/True/None duplicates.
|
||||||
_remove_duplicates(prefixed_completions, completions)
|
_remove_duplicates(prefixed_completions, completions)
|
||||||
+ sorted(completions, key=lambda x: (x.name.startswith('__'),
|
+ sorted(completions, key=lambda x: (not x.name.startswith(self._like_name),
|
||||||
|
x.name.startswith('__'),
|
||||||
x.name.startswith('_'),
|
x.name.startswith('_'),
|
||||||
x.name.lower()))
|
x.name.lower()))
|
||||||
)
|
)
|
||||||
@@ -442,6 +455,7 @@ class Completion:
|
|||||||
- Having some doctest code that starts with `>>>`
|
- Having some doctest code that starts with `>>>`
|
||||||
- Having backticks that doesn't have whitespace inside it
|
- Having backticks that doesn't have whitespace inside it
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def iter_relevant_lines(lines):
|
def iter_relevant_lines(lines):
|
||||||
include_next_line = False
|
include_next_line = False
|
||||||
for l in code_lines:
|
for l in code_lines:
|
||||||
@@ -664,3 +678,19 @@ def search_in_module(inference_state, module_context, names, wanted_names,
|
|||||||
def_ = classes.Name(inference_state, n2)
|
def_ = classes.Name(inference_state, n2)
|
||||||
if not wanted_type or wanted_type == def_.type:
|
if not wanted_type or wanted_type == def_.type:
|
||||||
yield def_
|
yield def_
|
||||||
|
|
||||||
|
|
||||||
|
def extract_imported_names(node):
|
||||||
|
imported_names = []
|
||||||
|
|
||||||
|
if node.type in ['import_as_names', 'dotted_as_names', 'dotted_as_name', 'import_as_name']:
|
||||||
|
for index, child in enumerate(node.children):
|
||||||
|
if child.type == 'name':
|
||||||
|
if (index > 1 and node.children[index - 1].type == "keyword"
|
||||||
|
and node.children[index - 1].value == "as"):
|
||||||
|
continue
|
||||||
|
imported_names.append(child.value)
|
||||||
|
elif child.type in ('import_as_name', 'dotted_as_name'):
|
||||||
|
imported_names.extend(extract_imported_names(child))
|
||||||
|
|
||||||
|
return imported_names
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import hashlib
|
|||||||
import filecmp
|
import filecmp
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from shutil import which
|
from shutil import which
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from jedi.cache import memoize_method, time_cache
|
from jedi.cache import memoize_method, time_cache
|
||||||
from jedi.inference.compiled.subprocess import CompiledSubprocess, \
|
from jedi.inference.compiled.subprocess import CompiledSubprocess, \
|
||||||
@@ -15,9 +16,13 @@ from jedi.inference.compiled.subprocess import CompiledSubprocess, \
|
|||||||
|
|
||||||
import parso
|
import parso
|
||||||
|
|
||||||
_VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
if TYPE_CHECKING:
|
||||||
|
from jedi.inference import InferenceState
|
||||||
|
|
||||||
_SUPPORTED_PYTHONS = ['3.10', '3.9', '3.8', '3.7', '3.6']
|
|
||||||
|
_VersionInfo = namedtuple('VersionInfo', 'major minor micro') # type: ignore[name-match]
|
||||||
|
|
||||||
|
_SUPPORTED_PYTHONS = ['3.13', '3.12', '3.11', '3.10', '3.9', '3.8']
|
||||||
_SAFE_PATHS = ['/usr/bin', '/usr/local/bin']
|
_SAFE_PATHS = ['/usr/bin', '/usr/local/bin']
|
||||||
_CONDA_VAR = 'CONDA_PREFIX'
|
_CONDA_VAR = 'CONDA_PREFIX'
|
||||||
_CURRENT_VERSION = '%s.%s' % (sys.version_info.major, sys.version_info.minor)
|
_CURRENT_VERSION = '%s.%s' % (sys.version_info.major, sys.version_info.minor)
|
||||||
@@ -102,7 +107,10 @@ class Environment(_BaseEnvironment):
|
|||||||
version = '.'.join(str(i) for i in self.version_info)
|
version = '.'.join(str(i) for i in self.version_info)
|
||||||
return '<%s: %s in %s>' % (self.__class__.__name__, version, self.path)
|
return '<%s: %s in %s>' % (self.__class__.__name__, version, self.path)
|
||||||
|
|
||||||
def get_inference_state_subprocess(self, inference_state):
|
def get_inference_state_subprocess(
|
||||||
|
self,
|
||||||
|
inference_state: 'InferenceState',
|
||||||
|
) -> InferenceStateSubprocess:
|
||||||
return InferenceStateSubprocess(inference_state, self._get_subprocess())
|
return InferenceStateSubprocess(inference_state, self._get_subprocess())
|
||||||
|
|
||||||
@memoize_method
|
@memoize_method
|
||||||
@@ -134,7 +142,10 @@ class SameEnvironment(_SameEnvironmentMixin, Environment):
|
|||||||
|
|
||||||
|
|
||||||
class InterpreterEnvironment(_SameEnvironmentMixin, _BaseEnvironment):
|
class InterpreterEnvironment(_SameEnvironmentMixin, _BaseEnvironment):
|
||||||
def get_inference_state_subprocess(self, inference_state):
|
def get_inference_state_subprocess(
|
||||||
|
self,
|
||||||
|
inference_state: 'InferenceState',
|
||||||
|
) -> InferenceStateSameProcess:
|
||||||
return InferenceStateSameProcess(inference_state)
|
return InferenceStateSameProcess(inference_state)
|
||||||
|
|
||||||
def get_sys_path(self):
|
def get_sys_path(self):
|
||||||
@@ -373,10 +384,13 @@ def _get_executable_path(path, safe=True):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
python = os.path.join(path, 'Scripts', 'python.exe')
|
pythons = [os.path.join(path, 'Scripts', 'python.exe'), os.path.join(path, 'python.exe')]
|
||||||
|
else:
|
||||||
|
pythons = [os.path.join(path, 'bin', 'python')]
|
||||||
|
for python in pythons:
|
||||||
|
if os.path.exists(python):
|
||||||
|
break
|
||||||
else:
|
else:
|
||||||
python = os.path.join(path, 'bin', 'python')
|
|
||||||
if not os.path.exists(python):
|
|
||||||
raise InvalidPythonEnvironment("%s seems to be missing." % python)
|
raise InvalidPythonEnvironment("%s seems to be missing." % python)
|
||||||
|
|
||||||
_assert_safe(python, safe)
|
_assert_safe(python, safe)
|
||||||
@@ -384,8 +398,7 @@ def _get_executable_path(path, safe=True):
|
|||||||
|
|
||||||
|
|
||||||
def _get_executables_from_windows_registry(version):
|
def _get_executables_from_windows_registry(version):
|
||||||
# https://github.com/python/typeshed/pull/3794 adds winreg
|
import winreg
|
||||||
import winreg # type: ignore[import]
|
|
||||||
|
|
||||||
# TODO: support Python Anaconda.
|
# TODO: support Python Anaconda.
|
||||||
sub_keys = [
|
sub_keys = [
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ class RefactoringError(_JediError):
|
|||||||
Refactorings can fail for various reasons. So if you work with refactorings
|
Refactorings can fail for various reasons. So if you work with refactorings
|
||||||
like :meth:`.Script.rename`, :meth:`.Script.inline`,
|
like :meth:`.Script.rename`, :meth:`.Script.inline`,
|
||||||
:meth:`.Script.extract_variable` and :meth:`.Script.extract_function`, make
|
:meth:`.Script.extract_variable` and :meth:`.Script.extract_function`, make
|
||||||
sure to catch these. The descriptions in the errors are ususally valuable
|
sure to catch these. The descriptions in the errors are usually valuable
|
||||||
for end users.
|
for end users.
|
||||||
|
|
||||||
A typical ``RefactoringError`` would tell the user that inlining is not
|
A typical ``RefactoringError`` would tell the user that inlining is not
|
||||||
|
|||||||
@@ -280,7 +280,7 @@ class CallDetails:
|
|||||||
def count_positional_arguments(self):
|
def count_positional_arguments(self):
|
||||||
count = 0
|
count = 0
|
||||||
for star_count, key_start, had_equal in self._list_arguments()[:-1]:
|
for star_count, key_start, had_equal in self._list_arguments()[:-1]:
|
||||||
if star_count:
|
if star_count or key_start:
|
||||||
break
|
break
|
||||||
count += 1
|
count += 1
|
||||||
return count
|
return count
|
||||||
@@ -306,7 +306,7 @@ def _iter_arguments(nodes, position):
|
|||||||
first = node.children[0]
|
first = node.children[0]
|
||||||
second = node.children[1]
|
second = node.children[1]
|
||||||
if second == '=':
|
if second == '=':
|
||||||
if second.start_pos < position:
|
if second.start_pos < position and first.type == 'name':
|
||||||
yield 0, first.value, True
|
yield 0, first.value, True
|
||||||
else:
|
else:
|
||||||
yield 0, remove_after_pos(first), False
|
yield 0, remove_after_pos(first), False
|
||||||
|
|||||||
@@ -5,8 +5,7 @@ from typing import Dict, Optional
|
|||||||
from jedi.inference.names import AbstractArbitraryName
|
from jedi.inference.names import AbstractArbitraryName
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# https://github.com/python/typeshed/pull/4351 adds pydoc_data
|
from pydoc_data import topics
|
||||||
from pydoc_data import topics # type: ignore[import]
|
|
||||||
pydoc_topics: Optional[Dict[str, str]] = topics.topics
|
pydoc_topics: Optional[Dict[str, str]] = topics.topics
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# Python 3.6.8 embeddable does not have pydoc_data.
|
# Python 3.6.8 embeddable does not have pydoc_data.
|
||||||
|
|||||||
@@ -352,9 +352,8 @@ class Project:
|
|||||||
# 3. Search for modules on sys.path
|
# 3. Search for modules on sys.path
|
||||||
sys_path = [
|
sys_path = [
|
||||||
p for p in self._get_sys_path(inference_state)
|
p for p in self._get_sys_path(inference_state)
|
||||||
# Exclude folders that are handled by recursing of the Python
|
# Exclude the current folder which is handled by recursing the folders.
|
||||||
# folders.
|
if p != self._path
|
||||||
if not p.startswith(str(self._path))
|
|
||||||
]
|
]
|
||||||
names = list(iter_module_names(inference_state, empty_module_context, sys_path))
|
names = list(iter_module_names(inference_state, empty_module_context, sys_path))
|
||||||
yield from search_in_module(
|
yield from search_in_module(
|
||||||
@@ -433,7 +432,6 @@ def get_default_project(path=None):
|
|||||||
probable_path = dir
|
probable_path = dir
|
||||||
|
|
||||||
if probable_path is not None:
|
if probable_path is not None:
|
||||||
# TODO search for setup.py etc
|
|
||||||
return Project(probable_path)
|
return Project(probable_path)
|
||||||
|
|
||||||
if first_no_init_file is not None:
|
if first_no_init_file is not None:
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ from typing import Dict, Iterable, Tuple
|
|||||||
from parso import split_lines
|
from parso import split_lines
|
||||||
|
|
||||||
from jedi.api.exceptions import RefactoringError
|
from jedi.api.exceptions import RefactoringError
|
||||||
|
from jedi.inference.value.namespace import ImplicitNSName
|
||||||
|
|
||||||
EXPRESSION_PARTS = (
|
EXPRESSION_PARTS = (
|
||||||
'or_test and_test not_test comparison '
|
'or_test and_test not_test comparison '
|
||||||
@@ -42,11 +43,17 @@ class ChangedFile:
|
|||||||
if self._from_path is None:
|
if self._from_path is None:
|
||||||
from_p = ''
|
from_p = ''
|
||||||
else:
|
else:
|
||||||
from_p = self._from_path.relative_to(project_path)
|
try:
|
||||||
|
from_p = self._from_path.relative_to(project_path)
|
||||||
|
except ValueError: # Happens it the path is not on th project_path
|
||||||
|
from_p = self._from_path
|
||||||
if self._to_path is None:
|
if self._to_path is None:
|
||||||
to_p = ''
|
to_p = ''
|
||||||
else:
|
else:
|
||||||
to_p = self._to_path.relative_to(project_path)
|
try:
|
||||||
|
to_p = self._to_path.relative_to(project_path)
|
||||||
|
except ValueError:
|
||||||
|
to_p = self._to_path
|
||||||
diff = difflib.unified_diff(
|
diff = difflib.unified_diff(
|
||||||
old_lines, new_lines,
|
old_lines, new_lines,
|
||||||
fromfile=str(from_p),
|
fromfile=str(from_p),
|
||||||
@@ -96,7 +103,12 @@ class Refactoring:
|
|||||||
to_path=calculate_to_path(path),
|
to_path=calculate_to_path(path),
|
||||||
module_node=next(iter(map_)).get_root_node(),
|
module_node=next(iter(map_)).get_root_node(),
|
||||||
node_to_str_map=map_
|
node_to_str_map=map_
|
||||||
) for path, map_ in sorted(self._file_to_node_changes.items())
|
)
|
||||||
|
# We need to use `or`, because the path can be None
|
||||||
|
for path, map_ in sorted(
|
||||||
|
self._file_to_node_changes.items(),
|
||||||
|
key=lambda x: x[0] or Path("")
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
def get_renames(self) -> Iterable[Tuple[Path, Path]]:
|
def get_renames(self) -> Iterable[Tuple[Path, Path]]:
|
||||||
@@ -110,7 +122,7 @@ class Refactoring:
|
|||||||
project_path = self._inference_state.project.path
|
project_path = self._inference_state.project.path
|
||||||
for from_, to in self.get_renames():
|
for from_, to in self.get_renames():
|
||||||
text += 'rename from %s\nrename to %s\n' \
|
text += 'rename from %s\nrename to %s\n' \
|
||||||
% (from_.relative_to(project_path), to.relative_to(project_path))
|
% (_try_relative_to(from_, project_path), _try_relative_to(to, project_path))
|
||||||
|
|
||||||
return text + ''.join(f.get_diff() for f in self.get_changed_files().values())
|
return text + ''.join(f.get_diff() for f in self.get_changed_files().values())
|
||||||
|
|
||||||
@@ -140,13 +152,16 @@ def rename(inference_state, definitions, new_name):
|
|||||||
raise RefactoringError("There is no name under the cursor")
|
raise RefactoringError("There is no name under the cursor")
|
||||||
|
|
||||||
for d in definitions:
|
for d in definitions:
|
||||||
|
# This private access is ok in a way. It's not public to
|
||||||
|
# protect Jedi users from seeing it.
|
||||||
tree_name = d._name.tree_name
|
tree_name = d._name.tree_name
|
||||||
if d.type == 'module' and tree_name is None:
|
if d.type == 'module' and tree_name is None and d.module_path is not None:
|
||||||
p = None if d.module_path is None else Path(d.module_path)
|
p = Path(d.module_path)
|
||||||
file_renames.add(_calculate_rename(p, new_name))
|
file_renames.add(_calculate_rename(p, new_name))
|
||||||
|
elif isinstance(d._name, ImplicitNSName):
|
||||||
|
for p in d._name._value.py__path__():
|
||||||
|
file_renames.add(_calculate_rename(Path(p), new_name))
|
||||||
else:
|
else:
|
||||||
# This private access is ok in a way. It's not public to
|
|
||||||
# protect Jedi users from seeing it.
|
|
||||||
if tree_name is not None:
|
if tree_name is not None:
|
||||||
fmap = file_tree_name_map.setdefault(d.module_path, {})
|
fmap = file_tree_name_map.setdefault(d.module_path, {})
|
||||||
fmap[tree_name] = tree_name.prefix + new_name
|
fmap[tree_name] = tree_name.prefix + new_name
|
||||||
@@ -240,3 +255,10 @@ def _remove_indent_of_prefix(prefix):
|
|||||||
Removes the last indentation of a prefix, e.g. " \n \n " becomes " \n \n".
|
Removes the last indentation of a prefix, e.g. " \n \n " becomes " \n \n".
|
||||||
"""
|
"""
|
||||||
return ''.join(split_lines(prefix, keepends=True)[:-1])
|
return ''.join(split_lines(prefix, keepends=True)[:-1])
|
||||||
|
|
||||||
|
|
||||||
|
def _try_relative_to(path: Path, base: Path) -> Path:
|
||||||
|
try:
|
||||||
|
return path.relative_to(base)
|
||||||
|
except ValueError:
|
||||||
|
return path
|
||||||
|
|||||||
@@ -36,8 +36,11 @@ def complete_dict(module_context, code_lines, leaf, position, string, fuzzy):
|
|||||||
string = cut_value_at_position(leaf, position)
|
string = cut_value_at_position(leaf, position)
|
||||||
|
|
||||||
context = module_context.create_context(bracket_leaf)
|
context = module_context.create_context(bracket_leaf)
|
||||||
before_bracket_leaf = bracket_leaf.get_previous_leaf()
|
|
||||||
if before_bracket_leaf.type in ('atom', 'trailer', 'name'):
|
before_node = before_bracket_leaf = bracket_leaf.get_previous_leaf()
|
||||||
|
if before_node in (')', ']', '}'):
|
||||||
|
before_node = before_node.parent
|
||||||
|
if before_node.type in ('atom', 'trailer', 'name'):
|
||||||
values = infer_call_of_leaf(context, before_bracket_leaf)
|
values = infer_call_of_leaf(context, before_bracket_leaf)
|
||||||
return list(_completions_for_dicts(
|
return list(_completions_for_dicts(
|
||||||
module_context.inference_state,
|
module_context.inference_state,
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ def clear_time_caches(delete_all: bool = False) -> None:
|
|||||||
:param delete_all: Deletes also the cache that is normally not deleted,
|
:param delete_all: Deletes also the cache that is normally not deleted,
|
||||||
like parser cache, which is important for faster parsing.
|
like parser cache, which is important for faster parsing.
|
||||||
"""
|
"""
|
||||||
global _time_caches
|
global _time_caches # noqa: F824
|
||||||
|
|
||||||
if delete_all:
|
if delete_all:
|
||||||
for cache in _time_caches.values():
|
for cache in _time_caches.values():
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ try:
|
|||||||
raise ImportError
|
raise ImportError
|
||||||
else:
|
else:
|
||||||
# Use colorama for nicer console output.
|
# Use colorama for nicer console output.
|
||||||
from colorama import Fore, init # type: ignore[import]
|
from colorama import Fore, init # type: ignore[import, unused-ignore]
|
||||||
from colorama import initialise
|
from colorama import initialise
|
||||||
|
|
||||||
def _lazy_colorama_init(): # noqa: F811
|
def _lazy_colorama_init(): # noqa: F811
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ class InferenceState:
|
|||||||
self.compiled_subprocess = environment.get_inference_state_subprocess(self)
|
self.compiled_subprocess = environment.get_inference_state_subprocess(self)
|
||||||
self.grammar = environment.get_grammar()
|
self.grammar = environment.get_grammar()
|
||||||
|
|
||||||
self.latest_grammar = parso.load_grammar(version='3.7')
|
self.latest_grammar = parso.load_grammar(version='3.13')
|
||||||
self.memoize_cache = {} # for memoize decorators
|
self.memoize_cache = {} # for memoize decorators
|
||||||
self.module_cache = imports.ModuleCache() # does the job of `sys.modules`.
|
self.module_cache = imports.ModuleCache() # does the job of `sys.modules`.
|
||||||
self.stub_module_cache = {} # Dict[Tuple[str, ...], Optional[ModuleValue]]
|
self.stub_module_cache = {} # Dict[Tuple[str, ...], Optional[ModuleValue]]
|
||||||
@@ -99,10 +99,11 @@ class InferenceState:
|
|||||||
self.mixed_cache = {} # see `inference.compiled.mixed._create()`
|
self.mixed_cache = {} # see `inference.compiled.mixed._create()`
|
||||||
self.analysis = []
|
self.analysis = []
|
||||||
self.dynamic_params_depth = 0
|
self.dynamic_params_depth = 0
|
||||||
|
self.do_dynamic_params_search = settings.dynamic_params
|
||||||
self.is_analysis = False
|
self.is_analysis = False
|
||||||
self.project = project
|
self.project = project
|
||||||
self.access_cache = {}
|
self.access_cache = {}
|
||||||
self.allow_descriptor_getattr = False
|
self.allow_unsafe_executions = False
|
||||||
self.flow_analysis_enabled = True
|
self.flow_analysis_enabled = True
|
||||||
|
|
||||||
self.reset_recursion_limitations()
|
self.reset_recursion_limitations()
|
||||||
@@ -121,14 +122,14 @@ class InferenceState:
|
|||||||
return value_set
|
return value_set
|
||||||
|
|
||||||
# mypy doesn't suppport decorated propeties (https://github.com/python/mypy/issues/1362)
|
# mypy doesn't suppport decorated propeties (https://github.com/python/mypy/issues/1362)
|
||||||
@property # type: ignore[misc]
|
@property
|
||||||
@inference_state_function_cache()
|
@inference_state_function_cache()
|
||||||
def builtins_module(self):
|
def builtins_module(self):
|
||||||
module_name = 'builtins'
|
module_name = 'builtins'
|
||||||
builtins_module, = self.import_module((module_name,), sys_path=())
|
builtins_module, = self.import_module((module_name,), sys_path=[])
|
||||||
return builtins_module
|
return builtins_module
|
||||||
|
|
||||||
@property # type: ignore[misc]
|
@property
|
||||||
@inference_state_function_cache()
|
@inference_state_function_cache()
|
||||||
def typing_module(self):
|
def typing_module(self):
|
||||||
typing_module, = self.import_module(('typing',))
|
typing_module, = self.import_module(('typing',))
|
||||||
|
|||||||
@@ -297,7 +297,7 @@ class Value(HelperValueMixin):
|
|||||||
just the `_T` generic parameter.
|
just the `_T` generic parameter.
|
||||||
|
|
||||||
`value_set`: represents the actual argument passed to the parameter
|
`value_set`: represents the actual argument passed to the parameter
|
||||||
we're inferrined for, or (for recursive calls) their types. In the
|
we're inferred for, or (for recursive calls) their types. In the
|
||||||
above example this would first be the representation of the list
|
above example this would first be the representation of the list
|
||||||
`[1]` and then, when recursing, just of `1`.
|
`[1]` and then, when recursing, just of `1`.
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import re
|
|||||||
import builtins
|
import builtins
|
||||||
import typing
|
import typing
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Optional, Tuple
|
||||||
|
|
||||||
from jedi.inference.compiled.getattr_static import getattr_static
|
from jedi.inference.compiled.getattr_static import getattr_static
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ NOT_CLASS_TYPES = (
|
|||||||
MethodDescriptorType = type(str.replace)
|
MethodDescriptorType = type(str.replace)
|
||||||
WrapperDescriptorType = type(set.__iter__)
|
WrapperDescriptorType = type(set.__iter__)
|
||||||
# `object.__subclasshook__` is an already executed descriptor.
|
# `object.__subclasshook__` is an already executed descriptor.
|
||||||
object_class_dict = type.__dict__["__dict__"].__get__(object)
|
object_class_dict = type.__dict__["__dict__"].__get__(object) # type: ignore[index]
|
||||||
ClassMethodDescriptorType = type(object_class_dict['__subclasshook__'])
|
ClassMethodDescriptorType = type(object_class_dict['__subclasshook__'])
|
||||||
|
|
||||||
_sentinel = object()
|
_sentinel = object()
|
||||||
@@ -147,7 +147,7 @@ class AccessPath:
|
|||||||
self.accesses = accesses
|
self.accesses = accesses
|
||||||
|
|
||||||
|
|
||||||
def create_access_path(inference_state, obj):
|
def create_access_path(inference_state, obj) -> AccessPath:
|
||||||
access = create_access(inference_state, obj)
|
access = create_access(inference_state, obj)
|
||||||
return AccessPath(access.get_access_path_tuples())
|
return AccessPath(access.get_access_path_tuples())
|
||||||
|
|
||||||
@@ -175,7 +175,7 @@ class DirectObjectAccess:
|
|||||||
def _create_access(self, obj):
|
def _create_access(self, obj):
|
||||||
return create_access(self._inference_state, obj)
|
return create_access(self._inference_state, obj)
|
||||||
|
|
||||||
def _create_access_path(self, obj):
|
def _create_access_path(self, obj) -> AccessPath:
|
||||||
return create_access_path(self._inference_state, obj)
|
return create_access_path(self._inference_state, obj)
|
||||||
|
|
||||||
def py__bool__(self):
|
def py__bool__(self):
|
||||||
@@ -230,8 +230,8 @@ class DirectObjectAccess:
|
|||||||
return [annotation]
|
return [annotation]
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def py__simple_getitem__(self, index):
|
def py__simple_getitem__(self, index, *, safe=True):
|
||||||
if type(self._obj) not in ALLOWED_GETITEM_TYPES:
|
if safe and type(self._obj) not in ALLOWED_GETITEM_TYPES:
|
||||||
# Get rid of side effects, we won't call custom `__getitem__`s.
|
# Get rid of side effects, we won't call custom `__getitem__`s.
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -329,33 +329,37 @@ class DirectObjectAccess:
|
|||||||
except TypeError:
|
except TypeError:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def is_allowed_getattr(self, name, safe=True):
|
def is_allowed_getattr(self, name, safe=True) -> Tuple[bool, bool, Optional[AccessPath]]:
|
||||||
# TODO this API is ugly.
|
# TODO this API is ugly.
|
||||||
if not safe:
|
|
||||||
# Unsafe is mostly used to check for __getattr__/__getattribute__.
|
|
||||||
# getattr_static works for properties, but the underscore methods
|
|
||||||
# are just ignored (because it's safer and avoids more code
|
|
||||||
# execution). See also GH #1378.
|
|
||||||
|
|
||||||
# Avoid warnings, see comment in the next function.
|
|
||||||
with warnings.catch_warnings(record=True):
|
|
||||||
warnings.simplefilter("always")
|
|
||||||
try:
|
|
||||||
return hasattr(self._obj, name), False
|
|
||||||
except Exception:
|
|
||||||
# Obviously has an attribute (propably a property) that
|
|
||||||
# gets executed, so just avoid all exceptions here.
|
|
||||||
return False, False
|
|
||||||
try:
|
try:
|
||||||
attr, is_get_descriptor = getattr_static(self._obj, name)
|
attr, is_get_descriptor = getattr_static(self._obj, name)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return False, False
|
if not safe:
|
||||||
|
# Unsafe is mostly used to check for __getattr__/__getattribute__.
|
||||||
|
# getattr_static works for properties, but the underscore methods
|
||||||
|
# are just ignored (because it's safer and avoids more code
|
||||||
|
# execution). See also GH #1378.
|
||||||
|
|
||||||
|
# Avoid warnings, see comment in the next function.
|
||||||
|
with warnings.catch_warnings(record=True):
|
||||||
|
warnings.simplefilter("always")
|
||||||
|
try:
|
||||||
|
return hasattr(self._obj, name), False, None
|
||||||
|
except Exception:
|
||||||
|
# Obviously has an attribute (probably a property) that
|
||||||
|
# gets executed, so just avoid all exceptions here.
|
||||||
|
pass
|
||||||
|
return False, False, None
|
||||||
else:
|
else:
|
||||||
if is_get_descriptor and type(attr) not in ALLOWED_DESCRIPTOR_ACCESS:
|
if is_get_descriptor and type(attr) not in ALLOWED_DESCRIPTOR_ACCESS:
|
||||||
|
if isinstance(attr, property):
|
||||||
|
if hasattr(attr.fget, '__annotations__'):
|
||||||
|
a = DirectObjectAccess(self._inference_state, attr.fget)
|
||||||
|
return True, True, a.get_return_annotation()
|
||||||
# In case of descriptors that have get methods we cannot return
|
# In case of descriptors that have get methods we cannot return
|
||||||
# it's value, because that would mean code execution.
|
# it's value, because that would mean code execution.
|
||||||
return True, True
|
return True, True, None
|
||||||
return True, False
|
return True, False, None
|
||||||
|
|
||||||
def getattr_paths(self, name, default=_sentinel):
|
def getattr_paths(self, name, default=_sentinel):
|
||||||
try:
|
try:
|
||||||
@@ -515,7 +519,7 @@ class DirectObjectAccess:
|
|||||||
# the signature. In that case we just want a simple escape for now.
|
# the signature. In that case we just want a simple escape for now.
|
||||||
raise ValueError
|
raise ValueError
|
||||||
|
|
||||||
def get_return_annotation(self):
|
def get_return_annotation(self) -> Optional[AccessPath]:
|
||||||
try:
|
try:
|
||||||
o = self._obj.__annotations__.get('return')
|
o = self._obj.__annotations__.get('return')
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ class MixedObject(ValueWrapper):
|
|||||||
|
|
||||||
This combined logic makes it possible to provide more powerful REPL
|
This combined logic makes it possible to provide more powerful REPL
|
||||||
completion. It allows side effects that are not noticable with the default
|
completion. It allows side effects that are not noticable with the default
|
||||||
parser structure to still be completeable.
|
parser structure to still be completable.
|
||||||
|
|
||||||
The biggest difference from CompiledValue to MixedObject is that we are
|
The biggest difference from CompiledValue to MixedObject is that we are
|
||||||
generally dealing with Python code and not with C code. This will generate
|
generally dealing with Python code and not with C code. This will generate
|
||||||
@@ -142,9 +142,9 @@ class MixedObjectFilter(compiled.CompiledValueFilter):
|
|||||||
super().__init__(inference_state, compiled_value)
|
super().__init__(inference_state, compiled_value)
|
||||||
self._tree_value = tree_value
|
self._tree_value = tree_value
|
||||||
|
|
||||||
def _create_name(self, name):
|
def _create_name(self, *args, **kwargs):
|
||||||
return MixedName(
|
return MixedName(
|
||||||
super()._create_name(name),
|
super()._create_name(*args, **kwargs),
|
||||||
self._tree_value,
|
self._tree_value,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -267,7 +267,7 @@ def _find_syntax_node_name(inference_state, python_object):
|
|||||||
@inference_state_function_cache()
|
@inference_state_function_cache()
|
||||||
def _create(inference_state, compiled_value, module_context):
|
def _create(inference_state, compiled_value, module_context):
|
||||||
# TODO accessing this is bad, but it probably doesn't matter that much,
|
# TODO accessing this is bad, but it probably doesn't matter that much,
|
||||||
# because we're working with interpreteters only here.
|
# because we're working with interpreters only here.
|
||||||
python_object = compiled_value.access_handle.access._obj
|
python_object = compiled_value.access_handle.access._obj
|
||||||
result = _find_syntax_node_name(inference_state, python_object)
|
result = _find_syntax_node_name(inference_state, python_object)
|
||||||
if result is None:
|
if result is None:
|
||||||
|
|||||||
@@ -5,6 +5,23 @@ goals:
|
|||||||
1. Making it safer - Segfaults and RuntimeErrors as well as stdout/stderr can
|
1. Making it safer - Segfaults and RuntimeErrors as well as stdout/stderr can
|
||||||
be ignored and dealt with.
|
be ignored and dealt with.
|
||||||
2. Make it possible to handle different Python versions as well as virtualenvs.
|
2. Make it possible to handle different Python versions as well as virtualenvs.
|
||||||
|
|
||||||
|
The architecture here is briefly:
|
||||||
|
- For each Jedi `Environment` there is a corresponding subprocess which
|
||||||
|
operates within the target environment. If the subprocess dies it is replaced
|
||||||
|
at this level.
|
||||||
|
- `CompiledSubprocess` manages exactly one subprocess and handles communication
|
||||||
|
from the parent side.
|
||||||
|
- `Listener` runs within the subprocess, processing each request and yielding
|
||||||
|
results.
|
||||||
|
- `InterpreterEnvironment` provides an API which matches that of `Environment`,
|
||||||
|
but runs functionality inline rather than within a subprocess. It is thus
|
||||||
|
used both directly in places where a subprocess is unnecessary and/or
|
||||||
|
undesirable and also within subprocesses themselves.
|
||||||
|
- `InferenceStateSubprocess` (or `InferenceStateSameProcess`) provide high
|
||||||
|
level access to functionality within the subprocess from within the parent.
|
||||||
|
Each `InterpreterState` has an instance of one of these, provided by its
|
||||||
|
environment.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import collections
|
import collections
|
||||||
@@ -16,6 +33,7 @@ import traceback
|
|||||||
import weakref
|
import weakref
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
from typing import Dict, TYPE_CHECKING
|
||||||
|
|
||||||
from jedi._compatibility import pickle_dump, pickle_load
|
from jedi._compatibility import pickle_dump, pickle_load
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
@@ -25,6 +43,9 @@ from jedi.inference.compiled.access import DirectObjectAccess, AccessPath, \
|
|||||||
SignatureParam
|
SignatureParam
|
||||||
from jedi.api.exceptions import InternalError
|
from jedi.api.exceptions import InternalError
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from jedi.inference import InferenceState
|
||||||
|
|
||||||
|
|
||||||
_MAIN_PATH = os.path.join(os.path.dirname(__file__), '__main__.py')
|
_MAIN_PATH = os.path.join(os.path.dirname(__file__), '__main__.py')
|
||||||
PICKLE_PROTOCOL = 4
|
PICKLE_PROTOCOL = 4
|
||||||
@@ -83,10 +104,9 @@ def _cleanup_process(process, thread):
|
|||||||
|
|
||||||
|
|
||||||
class _InferenceStateProcess:
|
class _InferenceStateProcess:
|
||||||
def __init__(self, inference_state):
|
def __init__(self, inference_state: 'InferenceState') -> None:
|
||||||
self._inference_state_weakref = weakref.ref(inference_state)
|
self._inference_state_weakref = weakref.ref(inference_state)
|
||||||
self._inference_state_id = id(inference_state)
|
self._handles: Dict[int, AccessHandle] = {}
|
||||||
self._handles = {}
|
|
||||||
|
|
||||||
def get_or_create_access_handle(self, obj):
|
def get_or_create_access_handle(self, obj):
|
||||||
id_ = id(obj)
|
id_ = id(obj)
|
||||||
@@ -116,11 +136,49 @@ class InferenceStateSameProcess(_InferenceStateProcess):
|
|||||||
|
|
||||||
|
|
||||||
class InferenceStateSubprocess(_InferenceStateProcess):
|
class InferenceStateSubprocess(_InferenceStateProcess):
|
||||||
def __init__(self, inference_state, compiled_subprocess):
|
"""
|
||||||
|
API to functionality which will run in a subprocess.
|
||||||
|
|
||||||
|
This mediates the interaction between an `InferenceState` and the actual
|
||||||
|
execution of functionality running within a `CompiledSubprocess`. Available
|
||||||
|
functions are defined in `.functions`, though should be accessed via
|
||||||
|
attributes on this class of the same name.
|
||||||
|
|
||||||
|
This class is responsible for indicating that the `InferenceState` within
|
||||||
|
the subprocess can be removed once the corresponding instance in the parent
|
||||||
|
goes away.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
inference_state: 'InferenceState',
|
||||||
|
compiled_subprocess: 'CompiledSubprocess',
|
||||||
|
) -> None:
|
||||||
super().__init__(inference_state)
|
super().__init__(inference_state)
|
||||||
self._used = False
|
self._used = False
|
||||||
self._compiled_subprocess = compiled_subprocess
|
self._compiled_subprocess = compiled_subprocess
|
||||||
|
|
||||||
|
# Opaque id we'll pass to the subprocess to identify the context (an
|
||||||
|
# `InferenceState`) which should be used for the request. This allows us
|
||||||
|
# to make subsequent requests which operate on results from previous
|
||||||
|
# ones, while keeping a single subprocess which can work with several
|
||||||
|
# contexts in the parent process. Once it is no longer needed(i.e: when
|
||||||
|
# this class goes away), we also use this id to indicate that the
|
||||||
|
# subprocess can discard the context.
|
||||||
|
#
|
||||||
|
# Note: this id is deliberately coupled to this class (and not to
|
||||||
|
# `InferenceState`) as this class manages access handle mappings which
|
||||||
|
# must correspond to those in the subprocess. This approach also avoids
|
||||||
|
# race conditions from successive `InferenceState`s with the same object
|
||||||
|
# id (as observed while adding support for Python 3.13).
|
||||||
|
#
|
||||||
|
# This value does not need to be the `id()` of this instance, we merely
|
||||||
|
# need to ensure that it enables the (visible) lifetime of the context
|
||||||
|
# within the subprocess to match that of this class. We therefore also
|
||||||
|
# depend on the semantics of `CompiledSubprocess.delete_inference_state`
|
||||||
|
# for correctness.
|
||||||
|
self._inference_state_id = id(self)
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
func = _get_function(name)
|
func = _get_function(name)
|
||||||
|
|
||||||
@@ -128,7 +186,7 @@ class InferenceStateSubprocess(_InferenceStateProcess):
|
|||||||
self._used = True
|
self._used = True
|
||||||
|
|
||||||
result = self._compiled_subprocess.run(
|
result = self._compiled_subprocess.run(
|
||||||
self._inference_state_weakref(),
|
self._inference_state_id,
|
||||||
func,
|
func,
|
||||||
args=args,
|
args=args,
|
||||||
kwargs=kwargs,
|
kwargs=kwargs,
|
||||||
@@ -164,6 +222,17 @@ class InferenceStateSubprocess(_InferenceStateProcess):
|
|||||||
|
|
||||||
|
|
||||||
class CompiledSubprocess:
|
class CompiledSubprocess:
|
||||||
|
"""
|
||||||
|
A subprocess which runs inference within a target environment.
|
||||||
|
|
||||||
|
This class manages the interface to a single instance of such a process as
|
||||||
|
well as the lifecycle of the process itself. See `.__main__` and `Listener`
|
||||||
|
for the implementation of the subprocess and details of the protocol.
|
||||||
|
|
||||||
|
A single live instance of this is maintained by `jedi.api.environment.Environment`,
|
||||||
|
so that typically a single subprocess is used at a time.
|
||||||
|
"""
|
||||||
|
|
||||||
is_crashed = False
|
is_crashed = False
|
||||||
|
|
||||||
def __init__(self, executable, env_vars=None):
|
def __init__(self, executable, env_vars=None):
|
||||||
@@ -213,18 +282,18 @@ class CompiledSubprocess:
|
|||||||
t)
|
t)
|
||||||
return process
|
return process
|
||||||
|
|
||||||
def run(self, inference_state, function, args=(), kwargs={}):
|
def run(self, inference_state_id, function, args=(), kwargs={}):
|
||||||
# Delete old inference_states.
|
# Delete old inference_states.
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
inference_state_id = self._inference_state_deletion_queue.pop()
|
delete_id = self._inference_state_deletion_queue.pop()
|
||||||
except IndexError:
|
except IndexError:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
self._send(inference_state_id, None)
|
self._send(delete_id, None)
|
||||||
|
|
||||||
assert callable(function)
|
assert callable(function)
|
||||||
return self._send(id(inference_state), function, args, kwargs)
|
return self._send(inference_state_id, function, args, kwargs)
|
||||||
|
|
||||||
def get_sys_path(self):
|
def get_sys_path(self):
|
||||||
return self._send(None, functions.get_sys_path, (), {})
|
return self._send(None, functions.get_sys_path, (), {})
|
||||||
@@ -272,21 +341,65 @@ class CompiledSubprocess:
|
|||||||
|
|
||||||
def delete_inference_state(self, inference_state_id):
|
def delete_inference_state(self, inference_state_id):
|
||||||
"""
|
"""
|
||||||
Currently we are not deleting inference_state instantly. They only get
|
Indicate that an inference state (in the subprocess) is no longer
|
||||||
deleted once the subprocess is used again. It would probably a better
|
needed.
|
||||||
solution to move all of this into a thread. However, the memory usage
|
|
||||||
of a single inference_state shouldn't be that high.
|
The state corresponding to the given id will become inaccessible and the
|
||||||
|
id may safely be re-used to refer to a different context.
|
||||||
|
|
||||||
|
Note: it is not guaranteed that the corresponding state will actually be
|
||||||
|
deleted immediately.
|
||||||
"""
|
"""
|
||||||
# With an argument - the inference_state gets deleted.
|
# Warning: if changing the semantics of context deletion see the comment
|
||||||
|
# in `InferenceStateSubprocess.__init__` regarding potential race
|
||||||
|
# conditions.
|
||||||
|
|
||||||
|
# Currently we are not deleting the related state instantly. They only
|
||||||
|
# get deleted once the subprocess is used again. It would probably a
|
||||||
|
# better solution to move all of this into a thread. However, the memory
|
||||||
|
# usage of a single inference_state shouldn't be that high.
|
||||||
self._inference_state_deletion_queue.append(inference_state_id)
|
self._inference_state_deletion_queue.append(inference_state_id)
|
||||||
|
|
||||||
|
|
||||||
class Listener:
|
class Listener:
|
||||||
|
"""
|
||||||
|
Main loop for the subprocess which actually does the inference.
|
||||||
|
|
||||||
|
This class runs within the target environment. It listens to instructions
|
||||||
|
from the parent process, runs inference and returns the results.
|
||||||
|
|
||||||
|
The subprocess has a long lifetime and is expected to process several
|
||||||
|
requests, including for different `InferenceState` instances in the parent.
|
||||||
|
See `CompiledSubprocess` for the parent half of the system.
|
||||||
|
|
||||||
|
Communication is via pickled data sent serially over stdin and stdout.
|
||||||
|
Stderr is read only if the child process crashes.
|
||||||
|
|
||||||
|
The request protocol is a 4-tuple of:
|
||||||
|
* inference_state_id | None: an opaque identifier of the parent's
|
||||||
|
`InferenceState`. An `InferenceState` operating over an
|
||||||
|
`InterpreterEnvironment` is created within this process for each of
|
||||||
|
these, ensuring that each parent context has a corresponding context
|
||||||
|
here. This allows context to be persisted between requests. Unless
|
||||||
|
`None`, the local `InferenceState` will be passed to the given function
|
||||||
|
as the first positional argument.
|
||||||
|
* function | None: the function to run. This is expected to be a member of
|
||||||
|
`.functions`. `None` indicates that the corresponding inference state is
|
||||||
|
no longer needed and should be dropped.
|
||||||
|
* args: positional arguments to the `function`. If any of these are
|
||||||
|
`AccessHandle` instances they will be adapted to the local
|
||||||
|
`InferenceState` before being passed.
|
||||||
|
* kwargs: keyword arguments to the `function`. If any of these are
|
||||||
|
`AccessHandle` instances they will be adapted to the local
|
||||||
|
`InferenceState` before being passed.
|
||||||
|
|
||||||
|
The result protocol is a 3-tuple of either:
|
||||||
|
* (False, None, function result): if the function returns without error, or
|
||||||
|
* (True, traceback, exception): if the function raises an exception
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._inference_states = {}
|
self._inference_states = {}
|
||||||
# TODO refactor so we don't need to process anymore just handle
|
|
||||||
# controlling.
|
|
||||||
self._process = _InferenceStateProcess(Listener)
|
|
||||||
|
|
||||||
def _get_inference_state(self, function, inference_state_id):
|
def _get_inference_state(self, function, inference_state_id):
|
||||||
from jedi.inference import InferenceState
|
from jedi.inference import InferenceState
|
||||||
@@ -308,6 +421,9 @@ class Listener:
|
|||||||
if inference_state_id is None:
|
if inference_state_id is None:
|
||||||
return function(*args, **kwargs)
|
return function(*args, **kwargs)
|
||||||
elif function is None:
|
elif function is None:
|
||||||
|
# Warning: if changing the semantics of context deletion see the comment
|
||||||
|
# in `InferenceStateSubprocess.__init__` regarding potential race
|
||||||
|
# conditions.
|
||||||
del self._inference_states[inference_state_id]
|
del self._inference_states[inference_state_id]
|
||||||
else:
|
else:
|
||||||
inference_state = self._get_inference_state(function, inference_state_id)
|
inference_state = self._get_inference_state(function, inference_state_id)
|
||||||
@@ -348,7 +464,12 @@ class Listener:
|
|||||||
|
|
||||||
|
|
||||||
class AccessHandle:
|
class AccessHandle:
|
||||||
def __init__(self, subprocess, access, id_):
|
def __init__(
|
||||||
|
self,
|
||||||
|
subprocess: _InferenceStateProcess,
|
||||||
|
access: DirectObjectAccess,
|
||||||
|
id_: int,
|
||||||
|
) -> None:
|
||||||
self.access = access
|
self.access = access
|
||||||
self._subprocess = subprocess
|
self._subprocess = subprocess
|
||||||
self.id = id_
|
self.id = id_
|
||||||
|
|||||||
@@ -3,10 +3,6 @@ import sys
|
|||||||
from importlib.abc import MetaPathFinder
|
from importlib.abc import MetaPathFinder
|
||||||
from importlib.machinery import PathFinder
|
from importlib.machinery import PathFinder
|
||||||
|
|
||||||
# Remove the first entry, because it's simply a directory entry that equals
|
|
||||||
# this directory.
|
|
||||||
del sys.path[0]
|
|
||||||
|
|
||||||
|
|
||||||
def _get_paths():
|
def _get_paths():
|
||||||
# Get the path to jedi.
|
# Get the path to jedi.
|
||||||
@@ -21,11 +17,11 @@ class _ExactImporter(MetaPathFinder):
|
|||||||
def __init__(self, path_dct):
|
def __init__(self, path_dct):
|
||||||
self._path_dct = path_dct
|
self._path_dct = path_dct
|
||||||
|
|
||||||
def find_module(self, fullname, path=None):
|
def find_spec(self, fullname, path=None, target=None):
|
||||||
if path is None and fullname in self._path_dct:
|
if path is None and fullname in self._path_dct:
|
||||||
p = self._path_dct[fullname]
|
p = self._path_dct[fullname]
|
||||||
loader = PathFinder.find_module(fullname, path=[p])
|
spec = PathFinder.find_spec(fullname, path=[p], target=target)
|
||||||
return loader
|
return spec
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import sys
|
|||||||
import os
|
import os
|
||||||
import inspect
|
import inspect
|
||||||
import importlib
|
import importlib
|
||||||
import warnings
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from zipfile import ZipFile
|
from zipfile import ZipFile
|
||||||
from zipimport import zipimporter, ZipImportError
|
from zipimport import zipimporter, ZipImportError
|
||||||
@@ -151,7 +150,11 @@ def _find_module(string, path=None, full_name=None, is_global_search=True):
|
|||||||
|
|
||||||
spec = find_spec(string, p)
|
spec = find_spec(string, p)
|
||||||
if spec is not None:
|
if spec is not None:
|
||||||
|
if spec.origin == "frozen":
|
||||||
|
continue
|
||||||
|
|
||||||
loader = spec.loader
|
loader = spec.loader
|
||||||
|
|
||||||
if loader is None and not spec.has_location:
|
if loader is None and not spec.has_location:
|
||||||
# This is a namespace package.
|
# This is a namespace package.
|
||||||
full_name = string if not path else full_name
|
full_name = string if not path else full_name
|
||||||
@@ -163,17 +166,16 @@ def _find_module(string, path=None, full_name=None, is_global_search=True):
|
|||||||
|
|
||||||
|
|
||||||
def _find_module_py33(string, path=None, loader=None, full_name=None, is_global_search=True):
|
def _find_module_py33(string, path=None, loader=None, full_name=None, is_global_search=True):
|
||||||
loader = loader or importlib.machinery.PathFinder.find_module(string, path)
|
if not loader:
|
||||||
|
spec = importlib.machinery.PathFinder.find_spec(string, path)
|
||||||
|
if spec is not None:
|
||||||
|
loader = spec.loader
|
||||||
|
|
||||||
if loader is None and path is None: # Fallback to find builtins
|
if loader is None and path is None: # Fallback to find builtins
|
||||||
try:
|
try:
|
||||||
with warnings.catch_warnings(record=True):
|
spec = importlib.util.find_spec(string)
|
||||||
# Mute "DeprecationWarning: Use importlib.util.find_spec()
|
if spec is not None:
|
||||||
# instead." While we should replace that in the future, it's
|
loader = spec.loader
|
||||||
# probably good to wait until we deprecate Python 3.3, since
|
|
||||||
# it was added in Python 3.4 and find_loader hasn't been
|
|
||||||
# removed in 3.6.
|
|
||||||
loader = importlib.find_loader(string)
|
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
# See #491. Importlib might raise a ValueError, to avoid this, we
|
# See #491. Importlib might raise a ValueError, to avoid this, we
|
||||||
# just raise an ImportError to fix the issue.
|
# just raise an ImportError to fix the issue.
|
||||||
|
|||||||
@@ -51,7 +51,6 @@ class CompiledValue(Value):
|
|||||||
def py__call__(self, arguments):
|
def py__call__(self, arguments):
|
||||||
return_annotation = self.access_handle.get_return_annotation()
|
return_annotation = self.access_handle.get_return_annotation()
|
||||||
if return_annotation is not None:
|
if return_annotation is not None:
|
||||||
# TODO the return annotation may also be a string.
|
|
||||||
return create_from_access_path(
|
return create_from_access_path(
|
||||||
self.inference_state,
|
self.inference_state,
|
||||||
return_annotation
|
return_annotation
|
||||||
@@ -163,7 +162,10 @@ class CompiledValue(Value):
|
|||||||
def py__simple_getitem__(self, index):
|
def py__simple_getitem__(self, index):
|
||||||
with reraise_getitem_errors(IndexError, KeyError, TypeError):
|
with reraise_getitem_errors(IndexError, KeyError, TypeError):
|
||||||
try:
|
try:
|
||||||
access = self.access_handle.py__simple_getitem__(index)
|
access = self.access_handle.py__simple_getitem__(
|
||||||
|
index,
|
||||||
|
safe=not self.inference_state.allow_unsafe_executions
|
||||||
|
)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return super().py__simple_getitem__(index)
|
return super().py__simple_getitem__(index)
|
||||||
if access is None:
|
if access is None:
|
||||||
@@ -311,11 +313,12 @@ class CompiledModule(CompiledValue):
|
|||||||
|
|
||||||
|
|
||||||
class CompiledName(AbstractNameDefinition):
|
class CompiledName(AbstractNameDefinition):
|
||||||
def __init__(self, inference_state, parent_value, name):
|
def __init__(self, inference_state, parent_value, name, is_descriptor):
|
||||||
self._inference_state = inference_state
|
self._inference_state = inference_state
|
||||||
self.parent_context = parent_value.as_context()
|
self.parent_context = parent_value.as_context()
|
||||||
self._parent_value = parent_value
|
self._parent_value = parent_value
|
||||||
self.string_name = name
|
self.string_name = name
|
||||||
|
self.is_descriptor = is_descriptor
|
||||||
|
|
||||||
def py__doc__(self):
|
def py__doc__(self):
|
||||||
return self.infer_compiled_value().py__doc__()
|
return self.infer_compiled_value().py__doc__()
|
||||||
@@ -342,6 +345,11 @@ class CompiledName(AbstractNameDefinition):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def api_type(self):
|
def api_type(self):
|
||||||
|
if self.is_descriptor:
|
||||||
|
# In case of properties we want to avoid executions as much as
|
||||||
|
# possible. Since the api_type can be wrong for other reasons
|
||||||
|
# anyway, we just return instance here.
|
||||||
|
return "instance"
|
||||||
return self.infer_compiled_value().api_type
|
return self.infer_compiled_value().api_type
|
||||||
|
|
||||||
def infer(self):
|
def infer(self):
|
||||||
@@ -432,9 +440,10 @@ class CompiledValueFilter(AbstractFilter):
|
|||||||
|
|
||||||
def get(self, name):
|
def get(self, name):
|
||||||
access_handle = self.compiled_value.access_handle
|
access_handle = self.compiled_value.access_handle
|
||||||
|
safe = not self._inference_state.allow_unsafe_executions
|
||||||
return self._get(
|
return self._get(
|
||||||
name,
|
name,
|
||||||
lambda name, safe: access_handle.is_allowed_getattr(name, safe=safe),
|
lambda name: access_handle.is_allowed_getattr(name, safe=safe),
|
||||||
lambda name: name in access_handle.dir(),
|
lambda name: name in access_handle.dir(),
|
||||||
check_has_attribute=True
|
check_has_attribute=True
|
||||||
)
|
)
|
||||||
@@ -443,30 +452,34 @@ class CompiledValueFilter(AbstractFilter):
|
|||||||
"""
|
"""
|
||||||
To remove quite a few access calls we introduced the callback here.
|
To remove quite a few access calls we introduced the callback here.
|
||||||
"""
|
"""
|
||||||
if self._inference_state.allow_descriptor_getattr:
|
has_attribute, is_descriptor, property_return_annotation = allowed_getattr_callback(
|
||||||
pass
|
|
||||||
|
|
||||||
has_attribute, is_descriptor = allowed_getattr_callback(
|
|
||||||
name,
|
name,
|
||||||
safe=not self._inference_state.allow_descriptor_getattr
|
|
||||||
)
|
)
|
||||||
|
if property_return_annotation is not None:
|
||||||
|
values = create_from_access_path(
|
||||||
|
self._inference_state,
|
||||||
|
property_return_annotation
|
||||||
|
).execute_annotation()
|
||||||
|
if values:
|
||||||
|
return [CompiledValueName(v, name) for v in values]
|
||||||
|
|
||||||
if check_has_attribute and not has_attribute:
|
if check_has_attribute and not has_attribute:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
if (is_descriptor or not has_attribute) \
|
if (is_descriptor or not has_attribute) \
|
||||||
and not self._inference_state.allow_descriptor_getattr:
|
and not self._inference_state.allow_unsafe_executions:
|
||||||
return [self._get_cached_name(name, is_empty=True)]
|
return [self._get_cached_name(name, is_empty=True)]
|
||||||
|
|
||||||
if self.is_instance and not in_dir_callback(name):
|
if self.is_instance and not in_dir_callback(name):
|
||||||
return []
|
return []
|
||||||
return [self._get_cached_name(name)]
|
return [self._get_cached_name(name, is_descriptor=is_descriptor)]
|
||||||
|
|
||||||
@memoize_method
|
@memoize_method
|
||||||
def _get_cached_name(self, name, is_empty=False):
|
def _get_cached_name(self, name, is_empty=False, *, is_descriptor=False):
|
||||||
if is_empty:
|
if is_empty:
|
||||||
return EmptyCompiledName(self._inference_state, name)
|
return EmptyCompiledName(self._inference_state, name)
|
||||||
else:
|
else:
|
||||||
return self._create_name(name)
|
return self._create_name(name, is_descriptor=is_descriptor)
|
||||||
|
|
||||||
def values(self):
|
def values(self):
|
||||||
from jedi.inference.compiled import builtin_from_name
|
from jedi.inference.compiled import builtin_from_name
|
||||||
@@ -480,7 +493,7 @@ class CompiledValueFilter(AbstractFilter):
|
|||||||
for name in dir_infos:
|
for name in dir_infos:
|
||||||
names += self._get(
|
names += self._get(
|
||||||
name,
|
name,
|
||||||
lambda name, safe: dir_infos[name],
|
lambda name: dir_infos[name],
|
||||||
lambda name: name in dir_infos,
|
lambda name: name in dir_infos,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -490,11 +503,12 @@ class CompiledValueFilter(AbstractFilter):
|
|||||||
names += filter.values()
|
names += filter.values()
|
||||||
return names
|
return names
|
||||||
|
|
||||||
def _create_name(self, name):
|
def _create_name(self, name, is_descriptor):
|
||||||
return CompiledName(
|
return CompiledName(
|
||||||
self._inference_state,
|
self._inference_state,
|
||||||
self.compiled_value,
|
self.compiled_value,
|
||||||
name
|
name,
|
||||||
|
is_descriptor,
|
||||||
)
|
)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ def _get_numpy_doc_string_cls():
|
|||||||
global _numpy_doc_string_cache
|
global _numpy_doc_string_cache
|
||||||
if isinstance(_numpy_doc_string_cache, (ImportError, SyntaxError)):
|
if isinstance(_numpy_doc_string_cache, (ImportError, SyntaxError)):
|
||||||
raise _numpy_doc_string_cache
|
raise _numpy_doc_string_cache
|
||||||
from numpydoc.docscrape import NumpyDocString # type: ignore[import]
|
from numpydoc.docscrape import NumpyDocString # type: ignore[import, unused-ignore]
|
||||||
_numpy_doc_string_cache = NumpyDocString
|
_numpy_doc_string_cache = NumpyDocString
|
||||||
return _numpy_doc_string_cache
|
return _numpy_doc_string_cache
|
||||||
|
|
||||||
@@ -109,7 +109,7 @@ def _expand_typestr(type_str):
|
|||||||
yield type_str.split('of')[0]
|
yield type_str.split('of')[0]
|
||||||
# Check if type has is a set of valid literal values eg: {'C', 'F', 'A'}
|
# Check if type has is a set of valid literal values eg: {'C', 'F', 'A'}
|
||||||
elif type_str.startswith('{'):
|
elif type_str.startswith('{'):
|
||||||
node = parse(type_str, version='3.7').children[0]
|
node = parse(type_str, version='3.13').children[0]
|
||||||
if node.type == 'atom':
|
if node.type == 'atom':
|
||||||
for leaf in getattr(node.children[1], "children", []):
|
for leaf in getattr(node.children[1], "children", []):
|
||||||
if leaf.type == 'number':
|
if leaf.type == 'number':
|
||||||
|
|||||||
@@ -66,11 +66,11 @@ def dynamic_param_lookup(function_value, param_index):
|
|||||||
have to look for all calls to ``func`` to find out what ``foo`` possibly
|
have to look for all calls to ``func`` to find out what ``foo`` possibly
|
||||||
is.
|
is.
|
||||||
"""
|
"""
|
||||||
funcdef = function_value.tree_node
|
if not function_value.inference_state.do_dynamic_params_search:
|
||||||
|
|
||||||
if not settings.dynamic_params:
|
|
||||||
return NO_VALUES
|
return NO_VALUES
|
||||||
|
|
||||||
|
funcdef = function_value.tree_node
|
||||||
|
|
||||||
path = function_value.get_root_context().py__file__()
|
path = function_value.get_root_context().py__file__()
|
||||||
if path is not None and is_stdlib_path(path):
|
if path is not None and is_stdlib_path(path):
|
||||||
# We don't want to search for references in the stdlib. Usually people
|
# We don't want to search for references in the stdlib. Usually people
|
||||||
|
|||||||
@@ -402,6 +402,10 @@ def find_type_from_comment_hint_for(context, node, name):
|
|||||||
|
|
||||||
|
|
||||||
def find_type_from_comment_hint_with(context, node, name):
|
def find_type_from_comment_hint_with(context, node, name):
|
||||||
|
if len(node.children) > 4:
|
||||||
|
# In case there are multiple with_items, we do not want a type hint for
|
||||||
|
# now.
|
||||||
|
return []
|
||||||
assert len(node.children[1].children) == 3, \
|
assert len(node.children[1].children) == 3, \
|
||||||
"Can only be here when children[1] is 'foo() as f'"
|
"Can only be here when children[1] is 'foo() as f'"
|
||||||
varlist = node.children[1].children[2]
|
varlist = node.children[1].children[2]
|
||||||
|
|||||||
@@ -86,6 +86,8 @@ class StubFilter(ParserTreeFilter):
|
|||||||
# Imports in stub files are only public if they have an "as"
|
# Imports in stub files are only public if they have an "as"
|
||||||
# export.
|
# export.
|
||||||
definition = name.get_definition()
|
definition = name.get_definition()
|
||||||
|
if definition is None:
|
||||||
|
return False
|
||||||
if definition.type in ('import_from', 'import_name'):
|
if definition.type in ('import_from', 'import_name'):
|
||||||
if name.parent.type not in ('import_as_name', 'dotted_as_name'):
|
if name.parent.type not in ('import_as_name', 'dotted_as_name'):
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ _TYPE_ALIAS_TYPES = {
|
|||||||
'DefaultDict': 'collections.defaultdict',
|
'DefaultDict': 'collections.defaultdict',
|
||||||
'Deque': 'collections.deque',
|
'Deque': 'collections.deque',
|
||||||
}
|
}
|
||||||
_PROXY_TYPES = 'Optional Union ClassVar'.split()
|
_PROXY_TYPES = 'Optional Union ClassVar Annotated'.split()
|
||||||
|
|
||||||
|
|
||||||
class TypingModuleName(NameWrapper):
|
class TypingModuleName(NameWrapper):
|
||||||
@@ -113,7 +113,7 @@ class ProxyWithGenerics(BaseTypingClassWithGenerics):
|
|||||||
elif string_name == 'Type':
|
elif string_name == 'Type':
|
||||||
# The type is actually already given in the index_value
|
# The type is actually already given in the index_value
|
||||||
return self._generics_manager[0]
|
return self._generics_manager[0]
|
||||||
elif string_name == 'ClassVar':
|
elif string_name in ['ClassVar', 'Annotated']:
|
||||||
# For now don't do anything here, ClassVars are always used.
|
# For now don't do anything here, ClassVars are always used.
|
||||||
return self._generics_manager[0].execute_annotation()
|
return self._generics_manager[0].execute_annotation()
|
||||||
|
|
||||||
@@ -294,6 +294,9 @@ class Callable(BaseTypingInstance):
|
|||||||
from jedi.inference.gradual.annotation import infer_return_for_callable
|
from jedi.inference.gradual.annotation import infer_return_for_callable
|
||||||
return infer_return_for_callable(arguments, param_values, result_values)
|
return infer_return_for_callable(arguments, param_values, result_values)
|
||||||
|
|
||||||
|
def py__get__(self, instance, class_value):
|
||||||
|
return ValueSet([self])
|
||||||
|
|
||||||
|
|
||||||
class Tuple(BaseTypingInstance):
|
class Tuple(BaseTypingInstance):
|
||||||
def _is_homogenous(self):
|
def _is_homogenous(self):
|
||||||
|
|||||||
@@ -480,7 +480,7 @@ def _load_builtin_module(inference_state, import_names=None, sys_path=None):
|
|||||||
if sys_path is None:
|
if sys_path is None:
|
||||||
sys_path = inference_state.get_sys_path()
|
sys_path = inference_state.get_sys_path()
|
||||||
if not project._load_unsafe_extensions:
|
if not project._load_unsafe_extensions:
|
||||||
safe_paths = project._get_base_sys_path(inference_state)
|
safe_paths = set(project._get_base_sys_path(inference_state))
|
||||||
sys_path = [p for p in sys_path if p in safe_paths]
|
sys_path = [p for p in sys_path if p in safe_paths]
|
||||||
|
|
||||||
dotted_name = '.'.join(import_names)
|
dotted_name = '.'.join(import_names)
|
||||||
|
|||||||
@@ -248,7 +248,7 @@ class ValueNameMixin:
|
|||||||
|
|
||||||
def get_defining_qualified_value(self):
|
def get_defining_qualified_value(self):
|
||||||
context = self.parent_context
|
context = self.parent_context
|
||||||
if context.is_module() or context.is_class():
|
if context is not None and (context.is_module() or context.is_class()):
|
||||||
return self.parent_context.get_value() # Might be None
|
return self.parent_context.get_value() # Might be None
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
@@ -180,26 +180,34 @@ def _check_fs(inference_state, file_io, regex):
|
|||||||
return m.as_context()
|
return m.as_context()
|
||||||
|
|
||||||
|
|
||||||
def gitignored_lines(folder_io, file_io):
|
def gitignored_paths(folder_io, file_io):
|
||||||
ignored_paths = set()
|
ignored_paths_abs = set()
|
||||||
ignored_names = set()
|
ignored_paths_rel = set()
|
||||||
|
|
||||||
for l in file_io.read().splitlines():
|
for l in file_io.read().splitlines():
|
||||||
if not l or l.startswith(b'#'):
|
if not l or l.startswith(b'#') or l.startswith(b'!') or b'*' in l:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
p = l.decode('utf-8', 'ignore')
|
p = l.decode('utf-8', 'ignore').rstrip('/')
|
||||||
if p.startswith('/'):
|
if '/' in p:
|
||||||
name = p[1:]
|
name = p.lstrip('/')
|
||||||
if name.endswith(os.path.sep):
|
ignored_paths_abs.add(os.path.join(folder_io.path, name))
|
||||||
name = name[:-1]
|
|
||||||
ignored_paths.add(os.path.join(folder_io.path, name))
|
|
||||||
else:
|
else:
|
||||||
ignored_names.add(p)
|
name = p
|
||||||
return ignored_paths, ignored_names
|
ignored_paths_rel.add((folder_io.path, name))
|
||||||
|
|
||||||
|
return ignored_paths_abs, ignored_paths_rel
|
||||||
|
|
||||||
|
|
||||||
|
def expand_relative_ignore_paths(folder_io, relative_paths):
|
||||||
|
curr_path = folder_io.path
|
||||||
|
return {os.path.join(curr_path, p[1]) for p in relative_paths if curr_path.startswith(p[0])}
|
||||||
|
|
||||||
|
|
||||||
def recurse_find_python_folders_and_files(folder_io, except_paths=()):
|
def recurse_find_python_folders_and_files(folder_io, except_paths=()):
|
||||||
except_paths = set(except_paths)
|
except_paths = set(except_paths)
|
||||||
|
except_paths_relative = set()
|
||||||
|
|
||||||
for root_folder_io, folder_ios, file_ios in folder_io.walk():
|
for root_folder_io, folder_ios, file_ios in folder_io.walk():
|
||||||
# Delete folders that we don't want to iterate over.
|
# Delete folders that we don't want to iterate over.
|
||||||
for file_io in file_ios:
|
for file_io in file_ios:
|
||||||
@@ -209,14 +217,21 @@ def recurse_find_python_folders_and_files(folder_io, except_paths=()):
|
|||||||
yield None, file_io
|
yield None, file_io
|
||||||
|
|
||||||
if path.name == '.gitignore':
|
if path.name == '.gitignore':
|
||||||
ignored_paths, ignored_names = \
|
ignored_paths_abs, ignored_paths_rel = gitignored_paths(
|
||||||
gitignored_lines(root_folder_io, file_io)
|
root_folder_io, file_io
|
||||||
except_paths |= ignored_paths
|
)
|
||||||
|
except_paths |= ignored_paths_abs
|
||||||
|
except_paths_relative |= ignored_paths_rel
|
||||||
|
|
||||||
|
except_paths_relative_expanded = expand_relative_ignore_paths(
|
||||||
|
root_folder_io, except_paths_relative
|
||||||
|
)
|
||||||
|
|
||||||
folder_ios[:] = [
|
folder_ios[:] = [
|
||||||
folder_io
|
folder_io
|
||||||
for folder_io in folder_ios
|
for folder_io in folder_ios
|
||||||
if folder_io.path not in except_paths
|
if folder_io.path not in except_paths
|
||||||
|
and folder_io.path not in except_paths_relative_expanded
|
||||||
and folder_io.get_base_name() not in _IGNORE_FOLDERS
|
and folder_io.get_base_name() not in _IGNORE_FOLDERS
|
||||||
]
|
]
|
||||||
for folder_io in folder_ios:
|
for folder_io in folder_ios:
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
Functions inferring the syntax tree.
|
Functions inferring the syntax tree.
|
||||||
"""
|
"""
|
||||||
import copy
|
import copy
|
||||||
|
import itertools
|
||||||
|
|
||||||
from parso.python import tree
|
from parso.python import tree
|
||||||
|
|
||||||
@@ -250,6 +251,8 @@ def _infer_node(context, element):
|
|||||||
return NO_VALUES
|
return NO_VALUES
|
||||||
elif typ == 'namedexpr_test':
|
elif typ == 'namedexpr_test':
|
||||||
return context.infer_node(element.children[2])
|
return context.infer_node(element.children[2])
|
||||||
|
elif typ == 'star_expr':
|
||||||
|
return NO_VALUES
|
||||||
else:
|
else:
|
||||||
return infer_or_test(context, element)
|
return infer_or_test(context, element)
|
||||||
|
|
||||||
@@ -328,8 +331,8 @@ def infer_atom(context, atom):
|
|||||||
c = atom.children
|
c = atom.children
|
||||||
# Parentheses without commas are not tuples.
|
# Parentheses without commas are not tuples.
|
||||||
if c[0] == '(' and not len(c) == 2 \
|
if c[0] == '(' and not len(c) == 2 \
|
||||||
and not(c[1].type == 'testlist_comp'
|
and not (c[1].type == 'testlist_comp'
|
||||||
and len(c[1].children) > 1):
|
and len(c[1].children) > 1):
|
||||||
return context.infer_node(c[1])
|
return context.infer_node(c[1])
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -492,8 +495,10 @@ def infer_factor(value_set, operator):
|
|||||||
elif operator == 'not':
|
elif operator == 'not':
|
||||||
b = value.py__bool__()
|
b = value.py__bool__()
|
||||||
if b is None: # Uncertainty.
|
if b is None: # Uncertainty.
|
||||||
return
|
yield list(value.inference_state.builtins_module.py__getattribute__('bool')
|
||||||
yield compiled.create_simple_object(value.inference_state, not b)
|
.execute_annotation()).pop()
|
||||||
|
else:
|
||||||
|
yield compiled.create_simple_object(value.inference_state, not b)
|
||||||
else:
|
else:
|
||||||
yield value
|
yield value
|
||||||
|
|
||||||
@@ -515,10 +520,20 @@ def _literals_to_types(inference_state, result):
|
|||||||
|
|
||||||
def _infer_comparison(context, left_values, operator, right_values):
|
def _infer_comparison(context, left_values, operator, right_values):
|
||||||
state = context.inference_state
|
state = context.inference_state
|
||||||
|
if isinstance(operator, str):
|
||||||
|
operator_str = operator
|
||||||
|
else:
|
||||||
|
operator_str = str(operator.value)
|
||||||
if not left_values or not right_values:
|
if not left_values or not right_values:
|
||||||
# illegal slices e.g. cause left/right_result to be None
|
# illegal slices e.g. cause left/right_result to be None
|
||||||
result = (left_values or NO_VALUES) | (right_values or NO_VALUES)
|
result = (left_values or NO_VALUES) | (right_values or NO_VALUES)
|
||||||
return _literals_to_types(state, result)
|
return _literals_to_types(state, result)
|
||||||
|
elif operator_str == "|" and all(
|
||||||
|
value.is_class() or value.is_compiled()
|
||||||
|
for value in itertools.chain(left_values, right_values)
|
||||||
|
):
|
||||||
|
# ^^^ A naive hack for PEP 604
|
||||||
|
return ValueSet.from_sets((left_values, right_values))
|
||||||
else:
|
else:
|
||||||
# I don't think there's a reasonable chance that a string
|
# I don't think there's a reasonable chance that a string
|
||||||
# operation is still correct, once we pass something like six
|
# operation is still correct, once we pass something like six
|
||||||
@@ -634,7 +649,7 @@ def _infer_comparison_part(inference_state, context, left, operator, right):
|
|||||||
_bool_to_value(inference_state, False)
|
_bool_to_value(inference_state, False)
|
||||||
])
|
])
|
||||||
elif str_operator in ('in', 'not in'):
|
elif str_operator in ('in', 'not in'):
|
||||||
return NO_VALUES
|
return inference_state.builtins_module.py__getattribute__('bool').execute_annotation()
|
||||||
|
|
||||||
def check(obj):
|
def check(obj):
|
||||||
"""Checks if a Jedi object is either a float or an int."""
|
"""Checks if a Jedi object is either a float or an int."""
|
||||||
@@ -684,8 +699,15 @@ def tree_name_to_values(inference_state, context, tree_name):
|
|||||||
|
|
||||||
if expr_stmt.type == "expr_stmt" and expr_stmt.children[1].type == "annassign":
|
if expr_stmt.type == "expr_stmt" and expr_stmt.children[1].type == "annassign":
|
||||||
correct_scope = parser_utils.get_parent_scope(name) == context.tree_node
|
correct_scope = parser_utils.get_parent_scope(name) == context.tree_node
|
||||||
|
ann_assign = expr_stmt.children[1]
|
||||||
if correct_scope:
|
if correct_scope:
|
||||||
found_annotation = True
|
found_annotation = True
|
||||||
|
if (
|
||||||
|
(ann_assign.children[1].type == 'name')
|
||||||
|
and (ann_assign.children[1].value == tree_name.value)
|
||||||
|
and context.parent_context
|
||||||
|
):
|
||||||
|
context = context.parent_context
|
||||||
value_set |= annotation.infer_annotation(
|
value_set |= annotation.infer_annotation(
|
||||||
context, expr_stmt.children[1].children[1]
|
context, expr_stmt.children[1].children[1]
|
||||||
).execute_annotation()
|
).execute_annotation()
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ settings will stop this process.
|
|||||||
|
|
||||||
It is important to note that:
|
It is important to note that:
|
||||||
|
|
||||||
1. Array modfications work only in the current module.
|
1. Array modifications work only in the current module.
|
||||||
2. Jedi only checks Array additions; ``list.pop``, etc are ignored.
|
2. Jedi only checks Array additions; ``list.pop``, etc are ignored.
|
||||||
"""
|
"""
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
|
|||||||
@@ -36,6 +36,10 @@ py__doc__() Returns the docstring for a value.
|
|||||||
====================================== ========================================
|
====================================== ========================================
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import List, Optional, Tuple
|
||||||
|
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi.parser_utils import get_cached_parent_scope, expr_is_dotted, \
|
from jedi.parser_utils import get_cached_parent_scope, expr_is_dotted, \
|
||||||
function_is_property
|
function_is_property
|
||||||
@@ -47,11 +51,15 @@ from jedi.inference.filters import ParserTreeFilter
|
|||||||
from jedi.inference.names import TreeNameDefinition, ValueName
|
from jedi.inference.names import TreeNameDefinition, ValueName
|
||||||
from jedi.inference.arguments import unpack_arglist, ValuesArguments
|
from jedi.inference.arguments import unpack_arglist, ValuesArguments
|
||||||
from jedi.inference.base_value import ValueSet, iterator_to_value_set, \
|
from jedi.inference.base_value import ValueSet, iterator_to_value_set, \
|
||||||
NO_VALUES
|
NO_VALUES, ValueWrapper
|
||||||
from jedi.inference.context import ClassContext
|
from jedi.inference.context import ClassContext
|
||||||
from jedi.inference.value.function import FunctionAndClassBase
|
from jedi.inference.value.function import FunctionAndClassBase, FunctionMixin
|
||||||
|
from jedi.inference.value.decorator import Decoratee
|
||||||
from jedi.inference.gradual.generics import LazyGenericManager, TupleGenericManager
|
from jedi.inference.gradual.generics import LazyGenericManager, TupleGenericManager
|
||||||
from jedi.plugins import plugin_manager
|
from jedi.plugins import plugin_manager
|
||||||
|
from inspect import Parameter
|
||||||
|
from jedi.inference.names import BaseTreeParamName
|
||||||
|
from jedi.inference.signature import AbstractSignature
|
||||||
|
|
||||||
|
|
||||||
class ClassName(TreeNameDefinition):
|
class ClassName(TreeNameDefinition):
|
||||||
@@ -78,6 +86,8 @@ class ClassName(TreeNameDefinition):
|
|||||||
type_ = super().api_type
|
type_ = super().api_type
|
||||||
if type_ == 'function':
|
if type_ == 'function':
|
||||||
definition = self.tree_name.get_definition()
|
definition = self.tree_name.get_definition()
|
||||||
|
if definition is None:
|
||||||
|
return type_
|
||||||
if function_is_property(definition):
|
if function_is_property(definition):
|
||||||
# This essentially checks if there is an @property before
|
# This essentially checks if there is an @property before
|
||||||
# the function. @property could be something different, but
|
# the function. @property could be something different, but
|
||||||
@@ -118,21 +128,6 @@ class ClassFilter(ParserTreeFilter):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def _access_possible(self, name):
|
def _access_possible(self, name):
|
||||||
# Filter for ClassVar variables
|
|
||||||
# TODO this is not properly done, yet. It just checks for the string
|
|
||||||
# ClassVar in the annotation, which can be quite imprecise. If we
|
|
||||||
# wanted to do this correct, we would have to infer the ClassVar.
|
|
||||||
if not self._is_instance:
|
|
||||||
expr_stmt = name.get_definition()
|
|
||||||
if expr_stmt is not None and expr_stmt.type == 'expr_stmt':
|
|
||||||
annassign = expr_stmt.children[1]
|
|
||||||
if annassign.type == 'annassign':
|
|
||||||
# If there is an =, the variable is obviously also
|
|
||||||
# defined on the class.
|
|
||||||
if 'ClassVar' not in annassign.children[1].get_code() \
|
|
||||||
and '=' not in annassign.children:
|
|
||||||
return False
|
|
||||||
|
|
||||||
# Filter for name mangling of private variables like __foo
|
# Filter for name mangling of private variables like __foo
|
||||||
return not name.value.startswith('__') or name.value.endswith('__') \
|
return not name.value.startswith('__') or name.value.endswith('__') \
|
||||||
or self._equals_origin_scope()
|
or self._equals_origin_scope()
|
||||||
@@ -142,6 +137,65 @@ class ClassFilter(ParserTreeFilter):
|
|||||||
return [name for name in names if self._access_possible(name)]
|
return [name for name in names if self._access_possible(name)]
|
||||||
|
|
||||||
|
|
||||||
|
def init_param_value(arg_nodes) -> Optional[bool]:
|
||||||
|
"""
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
- ``True`` if ``@dataclass(init=True)``
|
||||||
|
- ``False`` if ``@dataclass(init=False)``
|
||||||
|
- ``None`` if not specified ``@dataclass()``
|
||||||
|
"""
|
||||||
|
for arg_node in arg_nodes:
|
||||||
|
if (
|
||||||
|
arg_node.type == "argument"
|
||||||
|
and arg_node.children[0].value == "init"
|
||||||
|
):
|
||||||
|
if arg_node.children[2].value == "False":
|
||||||
|
return False
|
||||||
|
elif arg_node.children[2].value == "True":
|
||||||
|
return True
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def get_dataclass_param_names(cls) -> List[DataclassParamName]:
|
||||||
|
"""
|
||||||
|
``cls`` is a :class:`ClassMixin`. The type is only documented as mypy would
|
||||||
|
complain that some fields are missing.
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class A:
|
||||||
|
a: int
|
||||||
|
b: str = "toto"
|
||||||
|
|
||||||
|
For the previous example, the param names would be ``a`` and ``b``.
|
||||||
|
"""
|
||||||
|
param_names = []
|
||||||
|
filter_ = cls.as_context().get_global_filter()
|
||||||
|
for name in sorted(filter_.values(), key=lambda name: name.start_pos):
|
||||||
|
d = name.tree_name.get_definition()
|
||||||
|
annassign = d.children[1]
|
||||||
|
if d.type == 'expr_stmt' and annassign.type == 'annassign':
|
||||||
|
node = annassign.children[1]
|
||||||
|
if node.type == "atom_expr" and node.children[0].value == "ClassVar":
|
||||||
|
continue
|
||||||
|
|
||||||
|
if len(annassign.children) < 4:
|
||||||
|
default = None
|
||||||
|
else:
|
||||||
|
default = annassign.children[3]
|
||||||
|
|
||||||
|
param_names.append(DataclassParamName(
|
||||||
|
parent_context=cls.parent_context,
|
||||||
|
tree_name=name.tree_name,
|
||||||
|
annotation_node=annassign.children[1],
|
||||||
|
default_node=default,
|
||||||
|
))
|
||||||
|
return param_names
|
||||||
|
|
||||||
|
|
||||||
class ClassMixin:
|
class ClassMixin:
|
||||||
def is_class(self):
|
def is_class(self):
|
||||||
return True
|
return True
|
||||||
@@ -234,6 +288,73 @@ class ClassMixin:
|
|||||||
assert x is not None
|
assert x is not None
|
||||||
yield x
|
yield x
|
||||||
|
|
||||||
|
def _has_dataclass_transform_metaclasses(self) -> Tuple[bool, Optional[bool]]:
|
||||||
|
for meta in self.get_metaclasses(): # type: ignore[attr-defined]
|
||||||
|
if (
|
||||||
|
isinstance(meta, Decoratee)
|
||||||
|
# Internal leakage :|
|
||||||
|
and isinstance(meta._wrapped_value, DataclassTransformer)
|
||||||
|
):
|
||||||
|
return True, meta._wrapped_value.init_mode_from_new()
|
||||||
|
|
||||||
|
return False, None
|
||||||
|
|
||||||
|
def _get_dataclass_transform_signatures(self) -> List[DataclassSignature]:
|
||||||
|
"""
|
||||||
|
Returns: A non-empty list if the class has dataclass semantics else an
|
||||||
|
empty list.
|
||||||
|
|
||||||
|
The dataclass-like semantics will be assumed for any class that directly
|
||||||
|
or indirectly derives from the decorated class or uses the decorated
|
||||||
|
class as a metaclass.
|
||||||
|
"""
|
||||||
|
param_names = []
|
||||||
|
is_dataclass_transform = False
|
||||||
|
default_init_mode: Optional[bool] = None
|
||||||
|
for cls in reversed(list(self.py__mro__())):
|
||||||
|
if not is_dataclass_transform:
|
||||||
|
|
||||||
|
# If dataclass_transform is applied to a class, dataclass-like semantics
|
||||||
|
# will be assumed for any class that directly or indirectly derives from
|
||||||
|
# the decorated class or uses the decorated class as a metaclass.
|
||||||
|
if (
|
||||||
|
isinstance(cls, DataclassTransformer)
|
||||||
|
and cls.init_mode_from_init_subclass
|
||||||
|
):
|
||||||
|
is_dataclass_transform = True
|
||||||
|
default_init_mode = cls.init_mode_from_init_subclass
|
||||||
|
|
||||||
|
elif (
|
||||||
|
# Some object like CompiledValues would not be compatible
|
||||||
|
isinstance(cls, ClassMixin)
|
||||||
|
):
|
||||||
|
is_dataclass_transform, default_init_mode = (
|
||||||
|
cls._has_dataclass_transform_metaclasses()
|
||||||
|
)
|
||||||
|
|
||||||
|
# Attributes on the decorated class and its base classes are not
|
||||||
|
# considered to be fields.
|
||||||
|
if is_dataclass_transform:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# All inherited classes behave like dataclass semantics
|
||||||
|
if (
|
||||||
|
is_dataclass_transform
|
||||||
|
and isinstance(cls, ClassValue)
|
||||||
|
and (
|
||||||
|
cls.init_param_mode()
|
||||||
|
or (cls.init_param_mode() is None and default_init_mode)
|
||||||
|
)
|
||||||
|
):
|
||||||
|
param_names.extend(
|
||||||
|
get_dataclass_param_names(cls)
|
||||||
|
)
|
||||||
|
|
||||||
|
if is_dataclass_transform:
|
||||||
|
return [DataclassSignature(cls, param_names)]
|
||||||
|
else:
|
||||||
|
return []
|
||||||
|
|
||||||
def get_signatures(self):
|
def get_signatures(self):
|
||||||
# Since calling staticmethod without a function is illegal, the Jedi
|
# Since calling staticmethod without a function is illegal, the Jedi
|
||||||
# plugin doesn't return anything. Therefore call directly and get what
|
# plugin doesn't return anything. Therefore call directly and get what
|
||||||
@@ -245,7 +366,12 @@ class ClassMixin:
|
|||||||
return sigs
|
return sigs
|
||||||
args = ValuesArguments([])
|
args = ValuesArguments([])
|
||||||
init_funcs = self.py__call__(args).py__getattribute__('__init__')
|
init_funcs = self.py__call__(args).py__getattribute__('__init__')
|
||||||
return [sig.bind(self) for sig in init_funcs.get_signatures()]
|
|
||||||
|
dataclass_sigs = self._get_dataclass_transform_signatures()
|
||||||
|
if dataclass_sigs:
|
||||||
|
return dataclass_sigs
|
||||||
|
else:
|
||||||
|
return [sig.bind(self) for sig in init_funcs.get_signatures()]
|
||||||
|
|
||||||
def _as_context(self):
|
def _as_context(self):
|
||||||
return ClassContext(self)
|
return ClassContext(self)
|
||||||
@@ -332,6 +458,158 @@ class ClassMixin:
|
|||||||
return ValueSet({self})
|
return ValueSet({self})
|
||||||
|
|
||||||
|
|
||||||
|
class DataclassParamName(BaseTreeParamName):
|
||||||
|
"""
|
||||||
|
Represent a field declaration on a class with dataclass semantics.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, parent_context, tree_name, annotation_node, default_node):
|
||||||
|
super().__init__(parent_context, tree_name)
|
||||||
|
self.annotation_node = annotation_node
|
||||||
|
self.default_node = default_node
|
||||||
|
|
||||||
|
def get_kind(self):
|
||||||
|
return Parameter.POSITIONAL_OR_KEYWORD
|
||||||
|
|
||||||
|
def infer(self):
|
||||||
|
if self.annotation_node is None:
|
||||||
|
return NO_VALUES
|
||||||
|
else:
|
||||||
|
return self.parent_context.infer_node(self.annotation_node)
|
||||||
|
|
||||||
|
|
||||||
|
class DataclassSignature(AbstractSignature):
|
||||||
|
"""
|
||||||
|
It represents the ``__init__`` signature of a class with dataclass semantics.
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
"""
|
||||||
|
def __init__(self, value, param_names):
|
||||||
|
super().__init__(value)
|
||||||
|
self._param_names = param_names
|
||||||
|
|
||||||
|
def get_param_names(self, resolve_stars=False):
|
||||||
|
return self._param_names
|
||||||
|
|
||||||
|
|
||||||
|
class DataclassDecorator(ValueWrapper, FunctionMixin):
|
||||||
|
"""
|
||||||
|
A dataclass(-like) decorator with custom parameters.
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
@dataclass(init=True) # this
|
||||||
|
class A: ...
|
||||||
|
|
||||||
|
@dataclass_transform
|
||||||
|
def create_model(*, init=False): pass
|
||||||
|
|
||||||
|
@create_model(init=False) # or this
|
||||||
|
class B: ...
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, function, arguments, default_init: bool = True):
|
||||||
|
"""
|
||||||
|
Args:
|
||||||
|
function: Decoratee | function
|
||||||
|
arguments: The parameters to the dataclass function decorator
|
||||||
|
default_init: Boolean to indicate the default init value
|
||||||
|
"""
|
||||||
|
super().__init__(function)
|
||||||
|
argument_init = self._init_param_value(arguments)
|
||||||
|
self.init_param_mode = (
|
||||||
|
argument_init if argument_init is not None else default_init
|
||||||
|
)
|
||||||
|
|
||||||
|
def _init_param_value(self, arguments) -> Optional[bool]:
|
||||||
|
if not arguments.argument_node:
|
||||||
|
return None
|
||||||
|
|
||||||
|
arg_nodes = (
|
||||||
|
arguments.argument_node.children
|
||||||
|
if arguments.argument_node.type == "arglist"
|
||||||
|
else [arguments.argument_node]
|
||||||
|
)
|
||||||
|
|
||||||
|
return init_param_value(arg_nodes)
|
||||||
|
|
||||||
|
|
||||||
|
class DataclassTransformer(ValueWrapper, ClassMixin):
|
||||||
|
"""
|
||||||
|
A class decorated with the ``dataclass_transform`` decorator. dataclass-like
|
||||||
|
semantics will be assumed for any class that directly or indirectly derives
|
||||||
|
from the decorated class or uses the decorated class as a metaclass.
|
||||||
|
Attributes on the decorated class and its base classes are not considered to
|
||||||
|
be fields.
|
||||||
|
"""
|
||||||
|
def __init__(self, wrapped_value):
|
||||||
|
super().__init__(wrapped_value)
|
||||||
|
|
||||||
|
def init_mode_from_new(self) -> bool:
|
||||||
|
"""Default value if missing is ``True``"""
|
||||||
|
new_methods = self._wrapped_value.py__getattribute__("__new__")
|
||||||
|
|
||||||
|
if not new_methods:
|
||||||
|
return True
|
||||||
|
|
||||||
|
new_method = list(new_methods)[0]
|
||||||
|
|
||||||
|
for param in new_method.get_param_names():
|
||||||
|
if (
|
||||||
|
param.string_name == "init"
|
||||||
|
and param.default_node
|
||||||
|
and param.default_node.type == "keyword"
|
||||||
|
):
|
||||||
|
if param.default_node.value == "False":
|
||||||
|
return False
|
||||||
|
elif param.default_node.value == "True":
|
||||||
|
return True
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def init_mode_from_init_subclass(self) -> Optional[bool]:
|
||||||
|
# def __init_subclass__(cls) -> None: ... is hardcoded in the typeshed
|
||||||
|
# so the extra parameters can not be inferred.
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
class DataclassWrapper(ValueWrapper, ClassMixin):
|
||||||
|
"""
|
||||||
|
A class with dataclass semantics from a decorator. The init parameters are
|
||||||
|
only from the current class and parent classes decorated where the ``init``
|
||||||
|
parameter was ``True``.
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class A: ... # this
|
||||||
|
|
||||||
|
@dataclass_transform
|
||||||
|
def create_model(): pass
|
||||||
|
|
||||||
|
@create_model()
|
||||||
|
class B: ... # or this
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, wrapped_value, should_generate_init: bool
|
||||||
|
):
|
||||||
|
super().__init__(wrapped_value)
|
||||||
|
self.should_generate_init = should_generate_init
|
||||||
|
|
||||||
|
def get_signatures(self):
|
||||||
|
param_names = []
|
||||||
|
for cls in reversed(list(self.py__mro__())):
|
||||||
|
if (
|
||||||
|
isinstance(cls, DataclassWrapper)
|
||||||
|
and cls.should_generate_init
|
||||||
|
):
|
||||||
|
param_names.extend(get_dataclass_param_names(cls))
|
||||||
|
return [DataclassSignature(cls, param_names)]
|
||||||
|
|
||||||
|
|
||||||
class ClassValue(ClassMixin, FunctionAndClassBase, metaclass=CachedMetaClass):
|
class ClassValue(ClassMixin, FunctionAndClassBase, metaclass=CachedMetaClass):
|
||||||
api_type = 'class'
|
api_type = 'class'
|
||||||
|
|
||||||
@@ -398,6 +676,19 @@ class ClassValue(ClassMixin, FunctionAndClassBase, metaclass=CachedMetaClass):
|
|||||||
return values
|
return values
|
||||||
return NO_VALUES
|
return NO_VALUES
|
||||||
|
|
||||||
|
def init_param_mode(self) -> Optional[bool]:
|
||||||
|
"""
|
||||||
|
It returns ``True`` if ``class X(init=False):`` else ``False``.
|
||||||
|
"""
|
||||||
|
bases_arguments = self._get_bases_arguments()
|
||||||
|
|
||||||
|
if bases_arguments.argument_node.type != "arglist":
|
||||||
|
# If it is not inheriting from the base model and having
|
||||||
|
# extra parameters, then init behavior is not changed.
|
||||||
|
return None
|
||||||
|
|
||||||
|
return init_param_value(bases_arguments.argument_node.children)
|
||||||
|
|
||||||
@plugin_manager.decorate()
|
@plugin_manager.decorate()
|
||||||
def get_metaclass_signatures(self, metaclasses):
|
def get_metaclass_signatures(self, metaclasses):
|
||||||
return []
|
return []
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ class ModuleMixin(SubModuleDictMixin):
|
|||||||
def is_stub(self):
|
def is_stub(self):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@property # type: ignore[misc]
|
@property
|
||||||
@inference_state_method_cache()
|
@inference_state_method_cache()
|
||||||
def name(self):
|
def name(self):
|
||||||
return self._module_name_class(self, self.string_names[-1])
|
return self._module_name_class(self, self.string_names[-1])
|
||||||
@@ -138,7 +138,7 @@ class ModuleValue(ModuleMixin, TreeValue):
|
|||||||
api_type = 'module'
|
api_type = 'module'
|
||||||
|
|
||||||
def __init__(self, inference_state, module_node, code_lines, file_io=None,
|
def __init__(self, inference_state, module_node, code_lines, file_io=None,
|
||||||
string_names=None, is_package=False):
|
string_names=None, is_package=False) -> None:
|
||||||
super().__init__(
|
super().__init__(
|
||||||
inference_state,
|
inference_state,
|
||||||
parent_context=None,
|
parent_context=None,
|
||||||
@@ -149,7 +149,7 @@ class ModuleValue(ModuleMixin, TreeValue):
|
|||||||
self._path: Optional[Path] = None
|
self._path: Optional[Path] = None
|
||||||
else:
|
else:
|
||||||
self._path = file_io.path
|
self._path = file_io.path
|
||||||
self.string_names = string_names # Optional[Tuple[str, ...]]
|
self.string_names: Optional[tuple[str, ...]] = string_names
|
||||||
self.code_lines = code_lines
|
self.code_lines = code_lines
|
||||||
self._is_package = is_package
|
self._is_package = is_package
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ class ImplicitNamespaceValue(Value, SubModuleDictMixin):
|
|||||||
def get_qualified_names(self):
|
def get_qualified_names(self):
|
||||||
return ()
|
return ()
|
||||||
|
|
||||||
@property # type: ignore[misc]
|
@property
|
||||||
@inference_state_method_cache()
|
@inference_state_method_cache()
|
||||||
def name(self):
|
def name(self):
|
||||||
string_name = self.py__package__()[-1]
|
string_name = self.py__package__()[-1]
|
||||||
|
|||||||
@@ -320,7 +320,7 @@ def expr_is_dotted(node):
|
|||||||
return node.type == 'name'
|
return node.type == 'name'
|
||||||
|
|
||||||
|
|
||||||
def _function_is_x_method(*method_names):
|
def _function_is_x_method(decorator_checker):
|
||||||
def wrapper(function_node):
|
def wrapper(function_node):
|
||||||
"""
|
"""
|
||||||
This is a heuristic. It will not hold ALL the times, but it will be
|
This is a heuristic. It will not hold ALL the times, but it will be
|
||||||
@@ -330,12 +330,16 @@ def _function_is_x_method(*method_names):
|
|||||||
"""
|
"""
|
||||||
for decorator in function_node.get_decorators():
|
for decorator in function_node.get_decorators():
|
||||||
dotted_name = decorator.children[1]
|
dotted_name = decorator.children[1]
|
||||||
if dotted_name.get_code() in method_names:
|
if decorator_checker(dotted_name.get_code()):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
function_is_staticmethod = _function_is_x_method('staticmethod')
|
function_is_staticmethod = _function_is_x_method(lambda m: m == "staticmethod")
|
||||||
function_is_classmethod = _function_is_x_method('classmethod')
|
function_is_classmethod = _function_is_x_method(lambda m: m == "classmethod")
|
||||||
function_is_property = _function_is_x_method('property', 'cached_property')
|
function_is_property = _function_is_x_method(
|
||||||
|
lambda m: m == "property"
|
||||||
|
or m == "cached_property"
|
||||||
|
or (m.endswith(".setter"))
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
|
import sys
|
||||||
|
from typing import List
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from parso.tree import search_ancestor
|
from parso.tree import search_ancestor
|
||||||
from jedi.inference.cache import inference_state_method_cache
|
from jedi.inference.cache import inference_state_method_cache
|
||||||
from jedi.inference.imports import load_module_from_path
|
from jedi.inference.imports import goto_import, load_module_from_path
|
||||||
from jedi.inference.filters import ParserTreeFilter
|
from jedi.inference.filters import ParserTreeFilter
|
||||||
from jedi.inference.base_value import NO_VALUES, ValueSet
|
from jedi.inference.base_value import NO_VALUES, ValueSet
|
||||||
from jedi.inference.helpers import infer_call_of_leaf
|
from jedi.inference.helpers import infer_call_of_leaf
|
||||||
@@ -131,6 +133,36 @@ def _is_pytest_func(func_name, decorator_nodes):
|
|||||||
or any('fixture' in n.get_code() for n in decorator_nodes)
|
or any('fixture' in n.get_code() for n in decorator_nodes)
|
||||||
|
|
||||||
|
|
||||||
|
def _find_pytest_plugin_modules() -> List[List[str]]:
|
||||||
|
"""
|
||||||
|
Finds pytest plugin modules hooked by setuptools entry points
|
||||||
|
|
||||||
|
See https://docs.pytest.org/en/stable/how-to/writing_plugins.html#setuptools-entry-points
|
||||||
|
"""
|
||||||
|
if sys.version_info >= (3, 8):
|
||||||
|
from importlib.metadata import entry_points
|
||||||
|
|
||||||
|
if sys.version_info >= (3, 10):
|
||||||
|
pytest_entry_points = entry_points(group="pytest11")
|
||||||
|
else:
|
||||||
|
pytest_entry_points = entry_points().get("pytest11", ())
|
||||||
|
|
||||||
|
if sys.version_info >= (3, 9):
|
||||||
|
return [ep.module.split(".") for ep in pytest_entry_points]
|
||||||
|
else:
|
||||||
|
# Python 3.8 doesn't have `EntryPoint.module`. Implement equivalent
|
||||||
|
# to what Python 3.9 does (with additional None check to placate `mypy`)
|
||||||
|
matches = [
|
||||||
|
ep.pattern.match(ep.value)
|
||||||
|
for ep in pytest_entry_points
|
||||||
|
]
|
||||||
|
return [x.group('module').split(".") for x in matches if x]
|
||||||
|
|
||||||
|
else:
|
||||||
|
from pkg_resources import iter_entry_points
|
||||||
|
return [ep.module_name.split(".") for ep in iter_entry_points(group="pytest11")]
|
||||||
|
|
||||||
|
|
||||||
@inference_state_method_cache()
|
@inference_state_method_cache()
|
||||||
def _iter_pytest_modules(module_context, skip_own_module=False):
|
def _iter_pytest_modules(module_context, skip_own_module=False):
|
||||||
if not skip_own_module:
|
if not skip_own_module:
|
||||||
@@ -149,7 +181,13 @@ def _iter_pytest_modules(module_context, skip_own_module=False):
|
|||||||
if Path(file_io.path) != module_context.py__file__():
|
if Path(file_io.path) != module_context.py__file__():
|
||||||
try:
|
try:
|
||||||
m = load_module_from_path(module_context.inference_state, file_io)
|
m = load_module_from_path(module_context.inference_state, file_io)
|
||||||
yield m.as_context()
|
conftest_module = m.as_context()
|
||||||
|
yield conftest_module
|
||||||
|
|
||||||
|
plugins_list = m.tree_node.get_used_names().get("pytest_plugins")
|
||||||
|
if plugins_list:
|
||||||
|
name = conftest_module.create_name(plugins_list[0])
|
||||||
|
yield from _load_pytest_plugins(module_context, name)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
folder = folder.get_parent_folder()
|
folder = folder.get_parent_folder()
|
||||||
@@ -159,22 +197,49 @@ def _iter_pytest_modules(module_context, skip_own_module=False):
|
|||||||
break
|
break
|
||||||
last_folder = folder # keep track of the last found parent name
|
last_folder = folder # keep track of the last found parent name
|
||||||
|
|
||||||
for names in _PYTEST_FIXTURE_MODULES:
|
for names in _PYTEST_FIXTURE_MODULES + _find_pytest_plugin_modules():
|
||||||
for module_value in module_context.inference_state.import_module(names):
|
for module_value in module_context.inference_state.import_module(names):
|
||||||
yield module_value.as_context()
|
yield module_value.as_context()
|
||||||
|
|
||||||
|
|
||||||
|
def _load_pytest_plugins(module_context, name):
|
||||||
|
from jedi.inference.helpers import get_str_or_none
|
||||||
|
|
||||||
|
for inferred in name.infer():
|
||||||
|
for seq_value in inferred.py__iter__():
|
||||||
|
for value in seq_value.infer():
|
||||||
|
fq_name = get_str_or_none(value)
|
||||||
|
if fq_name:
|
||||||
|
names = fq_name.split(".")
|
||||||
|
for module_value in module_context.inference_state.import_module(names):
|
||||||
|
yield module_value.as_context()
|
||||||
|
|
||||||
|
|
||||||
class FixtureFilter(ParserTreeFilter):
|
class FixtureFilter(ParserTreeFilter):
|
||||||
def _filter(self, names):
|
def _filter(self, names):
|
||||||
for name in super()._filter(names):
|
for name in super()._filter(names):
|
||||||
funcdef = name.parent
|
# look for fixture definitions of imported names
|
||||||
# Class fixtures are not supported
|
if name.parent.type == "import_from":
|
||||||
if funcdef.type == 'funcdef':
|
imported_names = goto_import(self.parent_context, name)
|
||||||
decorated = funcdef.parent
|
if any(
|
||||||
if decorated.type == 'decorated' and self._is_fixture(decorated):
|
self._is_fixture(iname.parent_context, iname.tree_name)
|
||||||
|
for iname in imported_names
|
||||||
|
# discard imports of whole modules, that have no tree_name
|
||||||
|
if iname.tree_name
|
||||||
|
):
|
||||||
yield name
|
yield name
|
||||||
|
|
||||||
def _is_fixture(self, decorated):
|
elif self._is_fixture(self.parent_context, name):
|
||||||
|
yield name
|
||||||
|
|
||||||
|
def _is_fixture(self, context, name):
|
||||||
|
funcdef = name.parent
|
||||||
|
# Class fixtures are not supported
|
||||||
|
if funcdef.type != "funcdef":
|
||||||
|
return False
|
||||||
|
decorated = funcdef.parent
|
||||||
|
if decorated.type != "decorated":
|
||||||
|
return False
|
||||||
decorators = decorated.children[0]
|
decorators = decorated.children[0]
|
||||||
if decorators.type == 'decorators':
|
if decorators.type == 'decorators':
|
||||||
decorators = decorators.children
|
decorators = decorators.children
|
||||||
@@ -191,11 +256,12 @@ class FixtureFilter(ParserTreeFilter):
|
|||||||
last_leaf = last_trailer.get_last_leaf()
|
last_leaf = last_trailer.get_last_leaf()
|
||||||
if last_leaf == ')':
|
if last_leaf == ')':
|
||||||
values = infer_call_of_leaf(
|
values = infer_call_of_leaf(
|
||||||
self.parent_context, last_leaf, cut_own_trailer=True)
|
context, last_leaf, cut_own_trailer=True
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
values = self.parent_context.infer_node(dotted_name)
|
values = context.infer_node(dotted_name)
|
||||||
else:
|
else:
|
||||||
values = self.parent_context.infer_node(dotted_name)
|
values = context.infer_node(dotted_name)
|
||||||
for value in values:
|
for value in values:
|
||||||
if value.name.get_qualified_names(include_module_names=True) \
|
if value.name.get_qualified_names(include_module_names=True) \
|
||||||
== ('_pytest', 'fixtures', 'fixture'):
|
== ('_pytest', 'fixtures', 'fixture'):
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ compiled module that returns the types for C-builtins.
|
|||||||
"""
|
"""
|
||||||
import parso
|
import parso
|
||||||
import os
|
import os
|
||||||
from inspect import Parameter
|
|
||||||
|
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi.inference.utils import safe_property
|
from jedi.inference.utils import safe_property
|
||||||
@@ -25,15 +24,20 @@ from jedi.inference.value.instance import \
|
|||||||
from jedi.inference.base_value import ContextualizedNode, \
|
from jedi.inference.base_value import ContextualizedNode, \
|
||||||
NO_VALUES, ValueSet, ValueWrapper, LazyValueWrapper
|
NO_VALUES, ValueSet, ValueWrapper, LazyValueWrapper
|
||||||
from jedi.inference.value import ClassValue, ModuleValue
|
from jedi.inference.value import ClassValue, ModuleValue
|
||||||
from jedi.inference.value.klass import ClassMixin
|
from jedi.inference.value.decorator import Decoratee
|
||||||
|
from jedi.inference.value.klass import (
|
||||||
|
DataclassWrapper,
|
||||||
|
DataclassDecorator,
|
||||||
|
DataclassTransformer,
|
||||||
|
)
|
||||||
from jedi.inference.value.function import FunctionMixin
|
from jedi.inference.value.function import FunctionMixin
|
||||||
from jedi.inference.value import iterable
|
from jedi.inference.value import iterable
|
||||||
from jedi.inference.lazy_value import LazyTreeValue, LazyKnownValue, \
|
from jedi.inference.lazy_value import LazyTreeValue, LazyKnownValue, \
|
||||||
LazyKnownValues
|
LazyKnownValues
|
||||||
from jedi.inference.names import ValueName, BaseTreeParamName
|
from jedi.inference.names import ValueName
|
||||||
from jedi.inference.filters import AttributeOverwrite, publish_method, \
|
from jedi.inference.filters import AttributeOverwrite, publish_method, \
|
||||||
ParserTreeFilter, DictFilter
|
ParserTreeFilter, DictFilter
|
||||||
from jedi.inference.signature import AbstractSignature, SignatureWrapper
|
from jedi.inference.signature import SignatureWrapper
|
||||||
|
|
||||||
|
|
||||||
# Copied from Python 3.6's stdlib.
|
# Copied from Python 3.6's stdlib.
|
||||||
@@ -591,63 +595,101 @@ def _random_choice(sequences):
|
|||||||
|
|
||||||
|
|
||||||
def _dataclass(value, arguments, callback):
|
def _dataclass(value, arguments, callback):
|
||||||
|
"""
|
||||||
|
Decorator entry points for dataclass.
|
||||||
|
|
||||||
|
1. dataclass decorator declaration with parameters
|
||||||
|
2. dataclass semantics on a class from a dataclass(-like) decorator
|
||||||
|
"""
|
||||||
for c in _follow_param(value.inference_state, arguments, 0):
|
for c in _follow_param(value.inference_state, arguments, 0):
|
||||||
if c.is_class():
|
if c.is_class():
|
||||||
return ValueSet([DataclassWrapper(c)])
|
# Declare dataclass semantics on a class from a dataclass decorator
|
||||||
|
should_generate_init = (
|
||||||
|
# Customized decorator, init may be disabled
|
||||||
|
value.init_param_mode
|
||||||
|
if isinstance(value, DataclassDecorator)
|
||||||
|
# Bare dataclass decorator, always with init mode
|
||||||
|
else True
|
||||||
|
)
|
||||||
|
return ValueSet([DataclassWrapper(c, should_generate_init)])
|
||||||
else:
|
else:
|
||||||
return ValueSet([value])
|
# @dataclass(init=False)
|
||||||
|
# dataclass decorator customization
|
||||||
|
return ValueSet(
|
||||||
|
[
|
||||||
|
DataclassDecorator(
|
||||||
|
value,
|
||||||
|
arguments=arguments,
|
||||||
|
default_init=True,
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
return NO_VALUES
|
return NO_VALUES
|
||||||
|
|
||||||
|
|
||||||
class DataclassWrapper(ValueWrapper, ClassMixin):
|
def _dataclass_transform(value, arguments, callback):
|
||||||
def get_signatures(self):
|
"""
|
||||||
param_names = []
|
Decorator entry points for dataclass_transform.
|
||||||
for cls in reversed(list(self.py__mro__())):
|
|
||||||
if isinstance(cls, DataclassWrapper):
|
|
||||||
filter_ = cls.as_context().get_global_filter()
|
|
||||||
# .values ordering is not guaranteed, at least not in
|
|
||||||
# Python < 3.6, when dicts where not ordered, which is an
|
|
||||||
# implementation detail anyway.
|
|
||||||
for name in sorted(filter_.values(), key=lambda name: name.start_pos):
|
|
||||||
d = name.tree_name.get_definition()
|
|
||||||
annassign = d.children[1]
|
|
||||||
if d.type == 'expr_stmt' and annassign.type == 'annassign':
|
|
||||||
if len(annassign.children) < 4:
|
|
||||||
default = None
|
|
||||||
else:
|
|
||||||
default = annassign.children[3]
|
|
||||||
param_names.append(DataclassParamName(
|
|
||||||
parent_context=cls.parent_context,
|
|
||||||
tree_name=name.tree_name,
|
|
||||||
annotation_node=annassign.children[1],
|
|
||||||
default_node=default,
|
|
||||||
))
|
|
||||||
return [DataclassSignature(cls, param_names)]
|
|
||||||
|
|
||||||
|
1. dataclass-like decorator instantiation from a dataclass_transform decorator
|
||||||
|
2. dataclass_transform decorator declaration with parameters
|
||||||
|
3. dataclass-like decorator declaration with parameters
|
||||||
|
4. dataclass-like semantics on a class from a dataclass-like decorator
|
||||||
|
"""
|
||||||
|
for c in _follow_param(value.inference_state, arguments, 0):
|
||||||
|
if c.is_class():
|
||||||
|
is_dataclass_transform = (
|
||||||
|
value.name.string_name == "dataclass_transform"
|
||||||
|
# The decorator function from dataclass_transform acting as the
|
||||||
|
# dataclass decorator.
|
||||||
|
and not isinstance(value, Decoratee)
|
||||||
|
# The decorator function from dataclass_transform acting as the
|
||||||
|
# dataclass decorator with customized parameters
|
||||||
|
and not isinstance(value, DataclassDecorator)
|
||||||
|
)
|
||||||
|
|
||||||
class DataclassSignature(AbstractSignature):
|
if is_dataclass_transform:
|
||||||
def __init__(self, value, param_names):
|
# Declare base class
|
||||||
super().__init__(value)
|
return ValueSet([DataclassTransformer(c)])
|
||||||
self._param_names = param_names
|
else:
|
||||||
|
# Declare dataclass-like semantics on a class from a
|
||||||
def get_param_names(self, resolve_stars=False):
|
# dataclass-like decorator
|
||||||
return self._param_names
|
should_generate_init = value.init_param_mode
|
||||||
|
return ValueSet([DataclassWrapper(c, should_generate_init)])
|
||||||
|
elif c.is_function():
|
||||||
class DataclassParamName(BaseTreeParamName):
|
# dataclass-like decorator instantiation:
|
||||||
def __init__(self, parent_context, tree_name, annotation_node, default_node):
|
# @dataclass_transform
|
||||||
super().__init__(parent_context, tree_name)
|
# def create_model()
|
||||||
self.annotation_node = annotation_node
|
return ValueSet(
|
||||||
self.default_node = default_node
|
[
|
||||||
|
DataclassDecorator(
|
||||||
def get_kind(self):
|
value,
|
||||||
return Parameter.POSITIONAL_OR_KEYWORD
|
arguments=arguments,
|
||||||
|
default_init=True,
|
||||||
def infer(self):
|
)
|
||||||
if self.annotation_node is None:
|
]
|
||||||
return NO_VALUES
|
)
|
||||||
|
elif (
|
||||||
|
# @dataclass_transform
|
||||||
|
# def create_model(): pass
|
||||||
|
# @create_model(init=...)
|
||||||
|
isinstance(value, Decoratee)
|
||||||
|
):
|
||||||
|
# dataclass (or like) decorator customization
|
||||||
|
return ValueSet(
|
||||||
|
[
|
||||||
|
DataclassDecorator(
|
||||||
|
value,
|
||||||
|
arguments=arguments,
|
||||||
|
default_init=value._wrapped_value.init_param_mode,
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
return self.parent_context.infer_node(self.annotation_node)
|
# dataclass_transform decorator with parameters; nothing impactful
|
||||||
|
return ValueSet([value])
|
||||||
|
return NO_VALUES
|
||||||
|
|
||||||
|
|
||||||
class ItemGetterCallable(ValueWrapper):
|
class ItemGetterCallable(ValueWrapper):
|
||||||
@@ -798,6 +840,12 @@ _implemented = {
|
|||||||
# runtime_checkable doesn't really change anything and is just
|
# runtime_checkable doesn't really change anything and is just
|
||||||
# adding logs for infering stuff, so we can safely ignore it.
|
# adding logs for infering stuff, so we can safely ignore it.
|
||||||
'runtime_checkable': lambda value, arguments, callback: NO_VALUES,
|
'runtime_checkable': lambda value, arguments, callback: NO_VALUES,
|
||||||
|
# Python 3.11+
|
||||||
|
'dataclass_transform': _dataclass_transform,
|
||||||
|
},
|
||||||
|
'typing_extensions': {
|
||||||
|
# Python <3.11
|
||||||
|
'dataclass_transform': _dataclass_transform,
|
||||||
},
|
},
|
||||||
'dataclasses': {
|
'dataclasses': {
|
||||||
# For now this works at least better than Jedi trying to understand it.
|
# For now this works at least better than Jedi trying to understand it.
|
||||||
|
|||||||
@@ -143,6 +143,15 @@ This improves autocompletion for libraries that use ``setattr`` or
|
|||||||
``globals()`` modifications a lot.
|
``globals()`` modifications a lot.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
allow_unsafe_interpreter_executions = True
|
||||||
|
"""
|
||||||
|
Controls whether descriptors are evaluated when using an Interpreter. This is
|
||||||
|
something you might want to control when using Jedi from a Repl (e.g. IPython)
|
||||||
|
|
||||||
|
Generally this setting allows Jedi to execute __getitem__ and descriptors like
|
||||||
|
`property`.
|
||||||
|
"""
|
||||||
|
|
||||||
# ----------------
|
# ----------------
|
||||||
# Caching Validity
|
# Caching Validity
|
||||||
# ----------------
|
# ----------------
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
Utilities for end-users.
|
Utilities for end-users.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import __main__ # type: ignore[import]
|
import __main__
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
import logging
|
import logging
|
||||||
import traceback
|
import traceback
|
||||||
|
|||||||
14
setup.cfg
14
setup.cfg
@@ -21,13 +21,25 @@ per-file-ignores =
|
|||||||
jedi/__init__.py:F401
|
jedi/__init__.py:F401
|
||||||
jedi/inference/compiled/__init__.py:F401
|
jedi/inference/compiled/__init__.py:F401
|
||||||
jedi/inference/value/__init__.py:F401
|
jedi/inference/value/__init__.py:F401
|
||||||
exclude = jedi/third_party/* .tox/*
|
exclude =
|
||||||
|
.tox/*
|
||||||
|
jedi/third_party/*
|
||||||
|
test/completion/*
|
||||||
|
test/examples/*
|
||||||
|
test/refactor/*
|
||||||
|
test/static_analysis/*
|
||||||
|
|
||||||
[pycodestyle]
|
[pycodestyle]
|
||||||
max-line-length = 100
|
max-line-length = 100
|
||||||
|
|
||||||
|
|
||||||
[mypy]
|
[mypy]
|
||||||
|
# Exclude our copies of external stubs
|
||||||
|
exclude = ^jedi/third_party
|
||||||
|
|
||||||
|
show_error_codes = true
|
||||||
|
enable_error_code = ignore-without-code
|
||||||
|
|
||||||
# Ensure generics are explicit about what they are (e.g: `List[str]` rather than
|
# Ensure generics are explicit about what they are (e.g: `List[str]` rather than
|
||||||
# just `List`)
|
# just `List`)
|
||||||
disallow_any_generics = True
|
disallow_any_generics = True
|
||||||
|
|||||||
58
setup.py
58
setup.py
@@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
from typing import cast
|
||||||
|
|
||||||
from setuptools import setup, find_packages
|
from setuptools import setup, find_packages
|
||||||
from setuptools.depends import get_module_constant
|
from setuptools.depends import get_module_constant
|
||||||
@@ -9,7 +10,7 @@ __AUTHOR__ = 'David Halter'
|
|||||||
__AUTHOR_EMAIL__ = 'davidhalter88@gmail.com'
|
__AUTHOR_EMAIL__ = 'davidhalter88@gmail.com'
|
||||||
|
|
||||||
# Get the version from within jedi. It's defined in exactly one place now.
|
# Get the version from within jedi. It's defined in exactly one place now.
|
||||||
version = get_module_constant("jedi", "__version__")
|
version = cast(str, get_module_constant("jedi", "__version__"))
|
||||||
|
|
||||||
readme = open('README.rst').read() + '\n\n' + open('CHANGELOG.rst').read()
|
readme = open('README.rst').read() + '\n\n' + open('CHANGELOG.rst').read()
|
||||||
|
|
||||||
@@ -27,24 +28,62 @@ setup(name='jedi',
|
|||||||
maintainer=__AUTHOR__,
|
maintainer=__AUTHOR__,
|
||||||
maintainer_email=__AUTHOR_EMAIL__,
|
maintainer_email=__AUTHOR_EMAIL__,
|
||||||
url='https://github.com/davidhalter/jedi',
|
url='https://github.com/davidhalter/jedi',
|
||||||
|
project_urls={
|
||||||
|
"Documentation": 'https://jedi.readthedocs.io/en/latest/',
|
||||||
|
},
|
||||||
license='MIT',
|
license='MIT',
|
||||||
keywords='python completion refactoring vim',
|
keywords='python completion refactoring vim',
|
||||||
long_description=readme,
|
long_description=readme,
|
||||||
packages=find_packages(exclude=['test', 'test.*']),
|
packages=find_packages(exclude=['test', 'test.*']),
|
||||||
python_requires='>=3.6',
|
python_requires='>=3.8',
|
||||||
install_requires=['parso>=0.8.0,<0.9.0'],
|
# Python 3.13 grammars are added to parso in 0.8.4
|
||||||
|
install_requires=['parso>=0.8.4,<0.9.0'],
|
||||||
extras_require={
|
extras_require={
|
||||||
'testing': [
|
'testing': [
|
||||||
'pytest<7.0.0',
|
'pytest<9.0.0',
|
||||||
# docopt for sith doctests
|
# docopt for sith doctests
|
||||||
'docopt',
|
'docopt',
|
||||||
# coloroma for colored debug output
|
# coloroma for colored debug output
|
||||||
'colorama',
|
'colorama',
|
||||||
'Django<3.1', # For now pin this.
|
'Django',
|
||||||
|
'attrs',
|
||||||
|
'typing_extensions',
|
||||||
],
|
],
|
||||||
'qa': [
|
'qa': [
|
||||||
'flake8==3.8.3',
|
# latest version on 2025-06-16
|
||||||
'mypy==0.782',
|
'flake8==7.2.0',
|
||||||
|
# latest version supporting Python 3.6
|
||||||
|
'mypy==1.16',
|
||||||
|
# Arbitrary pins, latest at the time of pinning
|
||||||
|
'types-setuptools==80.9.0.20250529',
|
||||||
|
],
|
||||||
|
'docs': [
|
||||||
|
# Just pin all of these.
|
||||||
|
'Jinja2==2.11.3',
|
||||||
|
'MarkupSafe==1.1.1',
|
||||||
|
'Pygments==2.8.1',
|
||||||
|
'alabaster==0.7.12',
|
||||||
|
'babel==2.9.1',
|
||||||
|
'chardet==4.0.0',
|
||||||
|
'commonmark==0.8.1',
|
||||||
|
'docutils==0.17.1',
|
||||||
|
'future==0.18.2',
|
||||||
|
'idna==2.10',
|
||||||
|
'imagesize==1.2.0',
|
||||||
|
'mock==1.0.1',
|
||||||
|
'packaging==20.9',
|
||||||
|
'pyparsing==2.4.7',
|
||||||
|
'pytz==2021.1',
|
||||||
|
'readthedocs-sphinx-ext==2.1.4',
|
||||||
|
'recommonmark==0.5.0',
|
||||||
|
'requests==2.25.1',
|
||||||
|
'six==1.15.0',
|
||||||
|
'snowballstemmer==2.1.0',
|
||||||
|
'sphinx==1.8.5',
|
||||||
|
'sphinx-rtd-theme==0.4.3',
|
||||||
|
'sphinxcontrib-serializinghtml==1.1.4',
|
||||||
|
'sphinxcontrib-websupport==1.2.4',
|
||||||
|
'urllib3==1.26.4',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
package_data={'jedi': ['*.pyi', 'third_party/typeshed/LICENSE',
|
package_data={'jedi': ['*.pyi', 'third_party/typeshed/LICENSE',
|
||||||
@@ -57,11 +96,12 @@ setup(name='jedi',
|
|||||||
'License :: OSI Approved :: MIT License',
|
'License :: OSI Approved :: MIT License',
|
||||||
'Operating System :: OS Independent',
|
'Operating System :: OS Independent',
|
||||||
'Programming Language :: Python :: 3',
|
'Programming Language :: Python :: 3',
|
||||||
'Programming Language :: Python :: 3.6',
|
|
||||||
'Programming Language :: Python :: 3.7',
|
|
||||||
'Programming Language :: Python :: 3.8',
|
'Programming Language :: Python :: 3.8',
|
||||||
'Programming Language :: Python :: 3.9',
|
'Programming Language :: Python :: 3.9',
|
||||||
'Programming Language :: Python :: 3.10',
|
'Programming Language :: Python :: 3.10',
|
||||||
|
'Programming Language :: Python :: 3.11',
|
||||||
|
'Programming Language :: Python :: 3.12',
|
||||||
|
'Programming Language :: Python :: 3.13',
|
||||||
'Topic :: Software Development :: Libraries :: Python Modules',
|
'Topic :: Software Development :: Libraries :: Python Modules',
|
||||||
'Topic :: Text Editors :: Integrated Development Environments (IDE)',
|
'Topic :: Text Editors :: Integrated Development Environments (IDE)',
|
||||||
'Topic :: Utilities',
|
'Topic :: Utilities',
|
||||||
|
|||||||
2
sith.py
2
sith.py
@@ -44,7 +44,7 @@ Options:
|
|||||||
--pudb Launch pudb when error is raised.
|
--pudb Launch pudb when error is raised.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from docopt import docopt # type: ignore[import]
|
from docopt import docopt # type: ignore[import, unused-ignore]
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
|||||||
@@ -527,3 +527,11 @@ lc = [x for a, *x in [(1, '', 1.0)]]
|
|||||||
lc[0][0]
|
lc[0][0]
|
||||||
#?
|
#?
|
||||||
lc[0][1]
|
lc[0][1]
|
||||||
|
|
||||||
|
|
||||||
|
xy = (1,)
|
||||||
|
x, y = *xy, None
|
||||||
|
|
||||||
|
# whatever it is should not crash
|
||||||
|
#?
|
||||||
|
x
|
||||||
|
|||||||
@@ -47,8 +47,6 @@ async def awaitable_test():
|
|||||||
#? str()
|
#? str()
|
||||||
foo
|
foo
|
||||||
|
|
||||||
# python >= 3.6
|
|
||||||
|
|
||||||
async def asgen():
|
async def asgen():
|
||||||
yield 1
|
yield 1
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
|
|||||||
@@ -413,6 +413,10 @@ with Foo() as f3:
|
|||||||
with Foo() as f3:
|
with Foo() as f3:
|
||||||
f3
|
f3
|
||||||
|
|
||||||
|
with open("a"), open("b") as bfile:
|
||||||
|
#? ['flush']
|
||||||
|
bfile.flush
|
||||||
|
|
||||||
# -----------------
|
# -----------------
|
||||||
# Avoiding multiple definitions
|
# Avoiding multiple definitions
|
||||||
# -----------------
|
# -----------------
|
||||||
@@ -420,3 +424,13 @@ with Foo() as f3:
|
|||||||
some_array = ['', '']
|
some_array = ['', '']
|
||||||
#! ['def upper']
|
#! ['def upper']
|
||||||
some_array[some_not_defined_index].upper
|
some_array[some_not_defined_index].upper
|
||||||
|
|
||||||
|
# -----------------
|
||||||
|
# operator
|
||||||
|
# -----------------
|
||||||
|
|
||||||
|
#? bool()
|
||||||
|
res = 'f' in 'foo'; res
|
||||||
|
|
||||||
|
#? bool()
|
||||||
|
res = not {}; res
|
||||||
|
|||||||
@@ -23,7 +23,13 @@ def inheritance_fixture():
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def testdir(testdir):
|
def capsysbinary(capsysbinary):
|
||||||
#? ['chdir']
|
#? ['close']
|
||||||
testdir.chdir
|
capsysbinary.clos
|
||||||
return testdir
|
return capsysbinary
|
||||||
|
|
||||||
|
|
||||||
|
# used when fixtures are defined in multiple files
|
||||||
|
pytest_plugins = [
|
||||||
|
"completion.fixture_module",
|
||||||
|
]
|
||||||
|
|||||||
@@ -21,11 +21,9 @@ class Y(X):
|
|||||||
#? []
|
#? []
|
||||||
def __doc__
|
def __doc__
|
||||||
|
|
||||||
# This might or might not be what we wanted, currently properties are also
|
|
||||||
# used like this. IMO this is not wanted ~dave.
|
|
||||||
#? ['__class__']
|
|
||||||
def __class__
|
|
||||||
#? []
|
#? []
|
||||||
|
def __class__
|
||||||
|
#? ['__class__']
|
||||||
__class__
|
__class__
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import uuid
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.db.models.query_utils import DeferredAttribute
|
from django.db.models.query_utils import DeferredAttribute
|
||||||
|
from django.db.models.manager import BaseManager
|
||||||
|
|
||||||
|
|
||||||
class TagManager(models.Manager):
|
class TagManager(models.Manager):
|
||||||
|
|||||||
6
test/completion/fixture_module.py
Normal file
6
test/completion/fixture_module.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
# Exists only for completion/pytest.py
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def my_module_fixture():
|
||||||
|
return 1.0
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
# python >= 3.6
|
|
||||||
|
|
||||||
class Foo:
|
class Foo:
|
||||||
bar = 1
|
bar = 1
|
||||||
|
|
||||||
|
|||||||
@@ -110,4 +110,4 @@ class Test(object):
|
|||||||
# nocond lambdas make no sense at all.
|
# nocond lambdas make no sense at all.
|
||||||
|
|
||||||
#? int()
|
#? int()
|
||||||
[a for a in [1,2] if lambda: 3][0]
|
[a for a in [1,2] if (lambda: 3)][0]
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ from import_tree.pkg.mod1 import not_existant,
|
|||||||
#? 22 ['mod1', 'base']
|
#? 22 ['mod1', 'base']
|
||||||
from import_tree.pkg. import mod1
|
from import_tree.pkg. import mod1
|
||||||
#? 17 ['mod1', 'mod2', 'random', 'pkg', 'references', 'rename1', 'rename2', 'classes', 'globals', 'recurse_class1', 'recurse_class2', 'invisible_pkg', 'flow_import']
|
#? 17 ['mod1', 'mod2', 'random', 'pkg', 'references', 'rename1', 'rename2', 'classes', 'globals', 'recurse_class1', 'recurse_class2', 'invisible_pkg', 'flow_import']
|
||||||
from import_tree. import pkg
|
from import_tree. import new_pkg
|
||||||
|
|
||||||
#? 18 ['pkg']
|
#? 18 ['pkg']
|
||||||
from import_tree.p import pkg
|
from import_tree.p import pkg
|
||||||
|
|||||||
@@ -180,6 +180,11 @@ def argskwargs(*args: int, **kwargs: float):
|
|||||||
#? float()
|
#? float()
|
||||||
kwargs['']
|
kwargs['']
|
||||||
|
|
||||||
|
class Test:
|
||||||
|
str: str = 'abc'
|
||||||
|
|
||||||
|
#? ['upper']
|
||||||
|
Test.str.upp
|
||||||
|
|
||||||
class NotCalledClass:
|
class NotCalledClass:
|
||||||
def __init__(self, x):
|
def __init__(self, x):
|
||||||
|
|||||||
50
test/completion/pep0484_decorators.py
Normal file
50
test/completion/pep0484_decorators.py
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
""" Pep-0484 type hinted decorators """
|
||||||
|
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
|
|
||||||
|
def decorator(func):
|
||||||
|
def wrapper(*a, **k):
|
||||||
|
return str(func(*a, **k))
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def typed_decorator(func: Callable[..., int]) -> Callable[..., str]:
|
||||||
|
...
|
||||||
|
|
||||||
|
# Functions
|
||||||
|
|
||||||
|
@decorator
|
||||||
|
def plain_func() -> int:
|
||||||
|
return 4
|
||||||
|
|
||||||
|
#? str()
|
||||||
|
plain_func()
|
||||||
|
|
||||||
|
|
||||||
|
@typed_decorator
|
||||||
|
def typed_func() -> int:
|
||||||
|
return 4
|
||||||
|
|
||||||
|
#? str()
|
||||||
|
typed_func()
|
||||||
|
|
||||||
|
|
||||||
|
# Methods
|
||||||
|
|
||||||
|
class X:
|
||||||
|
@decorator
|
||||||
|
def plain_method(self) -> int:
|
||||||
|
return 4
|
||||||
|
|
||||||
|
@typed_decorator
|
||||||
|
def typed_method(self) -> int:
|
||||||
|
return 4
|
||||||
|
|
||||||
|
inst = X()
|
||||||
|
|
||||||
|
#? str()
|
||||||
|
inst.plain_method()
|
||||||
|
|
||||||
|
#? str()
|
||||||
|
inst.typed_method()
|
||||||
@@ -27,13 +27,13 @@ class PlainClass(object):
|
|||||||
|
|
||||||
|
|
||||||
tpl = ("1", 2)
|
tpl = ("1", 2)
|
||||||
tpl_typed = ("2", 3) # type: Tuple[str, int]
|
tpl_typed: Tuple[str, int] = ("2", 3)
|
||||||
|
|
||||||
collection = {"a": 1}
|
collection = {"a": 1}
|
||||||
collection_typed = {"a": 1} # type: Dict[str, int]
|
collection_typed: Dict[str, int] = {"a": 1}
|
||||||
|
|
||||||
list_of_ints = [42] # type: List[int]
|
list_of_ints: List[int] = [42]
|
||||||
list_of_funcs = [foo] # type: List[Callable[[T], T]]
|
list_of_funcs: List[Callable[[T], T]] = [foo]
|
||||||
|
|
||||||
custom_generic = CustomGeneric(123.45)
|
custom_generic = CustomGeneric(123.45)
|
||||||
|
|
||||||
|
|||||||
@@ -19,12 +19,12 @@ T_co = TypeVar('T_co', covariant=True)
|
|||||||
V = TypeVar('V')
|
V = TypeVar('V')
|
||||||
|
|
||||||
|
|
||||||
just_float = 42. # type: float
|
just_float: float = 42.
|
||||||
optional_float = 42. # type: Optional[float]
|
optional_float: Optional[float] = 42.
|
||||||
list_of_ints = [42] # type: List[int]
|
list_of_ints: List[int] = [42]
|
||||||
list_of_floats = [42.] # type: List[float]
|
list_of_floats: List[float] = [42.]
|
||||||
list_of_optional_floats = [x or None for x in list_of_floats] # type: List[Optional[float]]
|
list_of_optional_floats: List[Optional[float]] = [x or None for x in list_of_floats]
|
||||||
list_of_ints_and_strs = [42, 'abc'] # type: List[Union[int, str]]
|
list_of_ints_and_strs: List[Union[int, str]] = [42, 'abc']
|
||||||
|
|
||||||
# Test that simple parameters are handled
|
# Test that simple parameters are handled
|
||||||
def list_t_to_list_t(the_list: List[T]) -> List[T]:
|
def list_t_to_list_t(the_list: List[T]) -> List[T]:
|
||||||
@@ -48,7 +48,7 @@ for z in list_t_to_list_t(list_of_ints_and_strs):
|
|||||||
z
|
z
|
||||||
|
|
||||||
|
|
||||||
list_of_int_type = [int] # type: List[Type[int]]
|
list_of_int_type: List[Type[int]] = [int]
|
||||||
|
|
||||||
# Test that nested parameters are handled
|
# Test that nested parameters are handled
|
||||||
def list_optional_t_to_list_t(the_list: List[Optional[T]]) -> List[T]:
|
def list_optional_t_to_list_t(the_list: List[Optional[T]]) -> List[T]:
|
||||||
@@ -85,7 +85,7 @@ def optional_list_t_to_list_t(x: Optional[List[T]]) -> List[T]:
|
|||||||
return x if x is not None else []
|
return x if x is not None else []
|
||||||
|
|
||||||
|
|
||||||
optional_list_float = None # type: Optional[List[float]]
|
optional_list_float: Optional[List[float]] = None
|
||||||
for xc in optional_list_t_to_list_t(optional_list_float):
|
for xc in optional_list_t_to_list_t(optional_list_float):
|
||||||
#? float()
|
#? float()
|
||||||
xc
|
xc
|
||||||
@@ -134,7 +134,7 @@ def list_tuple_t_to_tuple_list_t(the_list: List[Tuple[T]]) -> Tuple[List[T], ...
|
|||||||
return tuple(list(x) for x in the_list)
|
return tuple(list(x) for x in the_list)
|
||||||
|
|
||||||
|
|
||||||
list_of_int_tuples = [(x,) for x in list_of_ints] # type: List[Tuple[int]]
|
list_of_int_tuples: List[Tuple[int]] = [(x,) for x in list_of_ints]
|
||||||
|
|
||||||
for b in list_tuple_t_to_tuple_list_t(list_of_int_tuples):
|
for b in list_tuple_t_to_tuple_list_t(list_of_int_tuples):
|
||||||
#? int()
|
#? int()
|
||||||
@@ -145,7 +145,7 @@ def list_tuple_t_elipsis_to_tuple_list_t(the_list: List[Tuple[T, ...]]) -> Tuple
|
|||||||
return tuple(list(x) for x in the_list)
|
return tuple(list(x) for x in the_list)
|
||||||
|
|
||||||
|
|
||||||
list_of_int_tuple_elipsis = [tuple(list_of_ints)] # type: List[Tuple[int, ...]]
|
list_of_int_tuple_elipsis: List[Tuple[int, ...]] = [tuple(list_of_ints)]
|
||||||
|
|
||||||
for b in list_tuple_t_elipsis_to_tuple_list_t(list_of_int_tuple_elipsis):
|
for b in list_tuple_t_elipsis_to_tuple_list_t(list_of_int_tuple_elipsis):
|
||||||
#? int()
|
#? int()
|
||||||
@@ -157,7 +157,7 @@ def foo(x: int) -> int:
|
|||||||
return x
|
return x
|
||||||
|
|
||||||
|
|
||||||
list_of_funcs = [foo] # type: List[Callable[[int], int]]
|
list_of_funcs: List[Callable[[int], int]] = [foo]
|
||||||
|
|
||||||
def list_func_t_to_list_func_type_t(the_list: List[Callable[[T], T]]) -> List[Callable[[Type[T]], T]]:
|
def list_func_t_to_list_func_type_t(the_list: List[Callable[[T], T]]) -> List[Callable[[Type[T]], T]]:
|
||||||
def adapt(func: Callable[[T], T]) -> Callable[[Type[T]], T]:
|
def adapt(func: Callable[[T], T]) -> Callable[[Type[T]], T]:
|
||||||
@@ -176,7 +176,7 @@ def bar(*a, **k) -> int:
|
|||||||
return len(a) + len(k)
|
return len(a) + len(k)
|
||||||
|
|
||||||
|
|
||||||
list_of_funcs_2 = [bar] # type: List[Callable[..., int]]
|
list_of_funcs_2: List[Callable[..., int]] = [bar]
|
||||||
|
|
||||||
def list_func_t_passthrough(the_list: List[Callable[..., T]]) -> List[Callable[..., T]]:
|
def list_func_t_passthrough(the_list: List[Callable[..., T]]) -> List[Callable[..., T]]:
|
||||||
return the_list
|
return the_list
|
||||||
@@ -187,7 +187,7 @@ for b in list_func_t_passthrough(list_of_funcs_2):
|
|||||||
b(None, x="x")
|
b(None, x="x")
|
||||||
|
|
||||||
|
|
||||||
mapping_int_str = {42: 'a'} # type: Dict[int, str]
|
mapping_int_str: Dict[int, str] = {42: 'a'}
|
||||||
|
|
||||||
# Test that mappings (that have more than one parameter) are handled
|
# Test that mappings (that have more than one parameter) are handled
|
||||||
def invert_mapping(mapping: Mapping[K, V]) -> Mapping[V, K]:
|
def invert_mapping(mapping: Mapping[K, V]) -> Mapping[V, K]:
|
||||||
@@ -210,11 +210,11 @@ first(mapping_int_str)
|
|||||||
#? str()
|
#? str()
|
||||||
first("abc")
|
first("abc")
|
||||||
|
|
||||||
some_str = NotImplemented # type: str
|
some_str: str = NotImplemented
|
||||||
#? str()
|
#? str()
|
||||||
first(some_str)
|
first(some_str)
|
||||||
|
|
||||||
annotated = [len] # type: List[ Callable[[Sequence[float]], int] ]
|
annotated: List[ Callable[[Sequence[float]], int] ] = [len]
|
||||||
#? int()
|
#? int()
|
||||||
first(annotated)()
|
first(annotated)()
|
||||||
|
|
||||||
@@ -237,7 +237,7 @@ for b in values(mapping_int_str):
|
|||||||
#
|
#
|
||||||
# Tests that user-defined generic types are handled
|
# Tests that user-defined generic types are handled
|
||||||
#
|
#
|
||||||
list_ints = [42] # type: List[int]
|
list_ints: List[int] = [42]
|
||||||
|
|
||||||
class CustomGeneric(Generic[T_co]):
|
class CustomGeneric(Generic[T_co]):
|
||||||
def __init__(self, val: T_co) -> None:
|
def __init__(self, val: T_co) -> None:
|
||||||
@@ -248,7 +248,7 @@ class CustomGeneric(Generic[T_co]):
|
|||||||
def custom(x: CustomGeneric[T]) -> T:
|
def custom(x: CustomGeneric[T]) -> T:
|
||||||
return x.val
|
return x.val
|
||||||
|
|
||||||
custom_instance = CustomGeneric(42) # type: CustomGeneric[int]
|
custom_instance: CustomGeneric[int] = CustomGeneric(42)
|
||||||
|
|
||||||
#? int()
|
#? int()
|
||||||
custom(custom_instance)
|
custom(custom_instance)
|
||||||
@@ -275,7 +275,7 @@ for x5 in wrap_custom(list_ints):
|
|||||||
|
|
||||||
|
|
||||||
# Test extraction of type from a nested custom generic type
|
# Test extraction of type from a nested custom generic type
|
||||||
list_custom_instances = [CustomGeneric(42)] # type: List[CustomGeneric[int]]
|
list_custom_instances: List[CustomGeneric[int]] = [CustomGeneric(42)]
|
||||||
|
|
||||||
def unwrap_custom(iterable: Iterable[CustomGeneric[T]]) -> List[T]:
|
def unwrap_custom(iterable: Iterable[CustomGeneric[T]]) -> List[T]:
|
||||||
return [x.val for x in iterable]
|
return [x.val for x in iterable]
|
||||||
@@ -303,7 +303,7 @@ for xg in unwrap_custom(CustomGeneric(s) for s in 'abc'):
|
|||||||
|
|
||||||
|
|
||||||
# Test extraction of type from type parameer nested within a custom generic type
|
# Test extraction of type from type parameer nested within a custom generic type
|
||||||
custom_instance_list_int = CustomGeneric([42]) # type: CustomGeneric[List[int]]
|
custom_instance_list_int: CustomGeneric[List[int]] = CustomGeneric([42])
|
||||||
|
|
||||||
def unwrap_custom2(instance: CustomGeneric[Iterable[T]]) -> List[T]:
|
def unwrap_custom2(instance: CustomGeneric[Iterable[T]]) -> List[T]:
|
||||||
return list(instance.val)
|
return list(instance.val)
|
||||||
@@ -326,7 +326,7 @@ class Specialised(Mapping[int, str]):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
specialised_instance = NotImplemented # type: Specialised
|
specialised_instance: Specialised = NotImplemented
|
||||||
|
|
||||||
#? int()
|
#? int()
|
||||||
first(specialised_instance)
|
first(specialised_instance)
|
||||||
@@ -341,7 +341,7 @@ class ChildOfSpecialised(Specialised):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
child_of_specialised_instance = NotImplemented # type: ChildOfSpecialised
|
child_of_specialised_instance: ChildOfSpecialised = NotImplemented
|
||||||
|
|
||||||
#? int()
|
#? int()
|
||||||
first(child_of_specialised_instance)
|
first(child_of_specialised_instance)
|
||||||
@@ -355,13 +355,13 @@ class CustomPartialGeneric1(Mapping[str, T]):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
custom_partial1_instance = NotImplemented # type: CustomPartialGeneric1[int]
|
custom_partial1_instance: CustomPartialGeneric1[int] = NotImplemented
|
||||||
|
|
||||||
#? str()
|
#? str()
|
||||||
first(custom_partial1_instance)
|
first(custom_partial1_instance)
|
||||||
|
|
||||||
|
|
||||||
custom_partial1_unbound_instance = NotImplemented # type: CustomPartialGeneric1
|
custom_partial1_unbound_instance: CustomPartialGeneric1 = NotImplemented
|
||||||
|
|
||||||
#? str()
|
#? str()
|
||||||
first(custom_partial1_unbound_instance)
|
first(custom_partial1_unbound_instance)
|
||||||
@@ -371,7 +371,7 @@ class CustomPartialGeneric2(Mapping[T, str]):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
custom_partial2_instance = NotImplemented # type: CustomPartialGeneric2[int]
|
custom_partial2_instance: CustomPartialGeneric2[int] = NotImplemented
|
||||||
|
|
||||||
#? int()
|
#? int()
|
||||||
first(custom_partial2_instance)
|
first(custom_partial2_instance)
|
||||||
@@ -380,7 +380,7 @@ first(custom_partial2_instance)
|
|||||||
values(custom_partial2_instance)[0]
|
values(custom_partial2_instance)[0]
|
||||||
|
|
||||||
|
|
||||||
custom_partial2_unbound_instance = NotImplemented # type: CustomPartialGeneric2
|
custom_partial2_unbound_instance: CustomPartialGeneric2 = NotImplemented
|
||||||
|
|
||||||
#? []
|
#? []
|
||||||
first(custom_partial2_unbound_instance)
|
first(custom_partial2_unbound_instance)
|
||||||
|
|||||||
@@ -19,16 +19,16 @@ TTypeAny = TypeVar('TTypeAny', bound=Type[Any])
|
|||||||
TCallable = TypeVar('TCallable', bound=Callable[..., Any])
|
TCallable = TypeVar('TCallable', bound=Callable[..., Any])
|
||||||
|
|
||||||
untyped_list_str = ['abc', 'def']
|
untyped_list_str = ['abc', 'def']
|
||||||
typed_list_str = ['abc', 'def'] # type: List[str]
|
typed_list_str: List[str] = ['abc', 'def']
|
||||||
|
|
||||||
untyped_tuple_str = ('abc',)
|
untyped_tuple_str = ('abc',)
|
||||||
typed_tuple_str = ('abc',) # type: Tuple[str]
|
typed_tuple_str: Tuple[str] = ('abc',)
|
||||||
|
|
||||||
untyped_tuple_str_int = ('abc', 4)
|
untyped_tuple_str_int = ('abc', 4)
|
||||||
typed_tuple_str_int = ('abc', 4) # type: Tuple[str, int]
|
typed_tuple_str_int: Tuple[str, int] = ('abc', 4)
|
||||||
|
|
||||||
variadic_tuple_str = ('abc',) # type: Tuple[str, ...]
|
variadic_tuple_str: Tuple[str, ...] = ('abc',)
|
||||||
variadic_tuple_str_int = ('abc', 4) # type: Tuple[Union[str, int], ...]
|
variadic_tuple_str_int: Tuple[Union[str, int], ...] = ('abc', 4)
|
||||||
|
|
||||||
|
|
||||||
def untyped_passthrough(x):
|
def untyped_passthrough(x):
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
# python >= 3.6
|
|
||||||
from typing import List, Dict, overload, Tuple, TypeVar
|
from typing import List, Dict, overload, Tuple, TypeVar
|
||||||
|
|
||||||
lst: list
|
lst: list
|
||||||
list_alias: List
|
list_alias: List
|
||||||
list_str: List[str]
|
list_str: List[str]
|
||||||
list_str: List[int]
|
list_int: List[int]
|
||||||
|
|
||||||
# -------------------------
|
# -------------------------
|
||||||
# With base classes
|
# With base classes
|
||||||
|
|||||||
@@ -2,18 +2,14 @@
|
|||||||
Test the typing library, with docstrings and annotations
|
Test the typing library, with docstrings and annotations
|
||||||
"""
|
"""
|
||||||
import typing
|
import typing
|
||||||
|
from typing import Sequence, MutableSequence, List, Iterable, Iterator, \
|
||||||
|
AbstractSet, Tuple, Mapping, Dict, Union, Optional
|
||||||
|
|
||||||
class B:
|
class B:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def we_can_has_sequence(p, q, r, s, t, u):
|
def we_can_has_sequence(p: Sequence[int], q: Sequence[B], r: Sequence[int],
|
||||||
"""
|
s: Sequence["int"], t: MutableSequence[dict], u: List[float]):
|
||||||
:type p: typing.Sequence[int]
|
|
||||||
:type q: typing.Sequence[B]
|
|
||||||
:type r: typing.Sequence[int]
|
|
||||||
:type s: typing.Sequence["int"]
|
|
||||||
:type t: typing.MutableSequence[dict]
|
|
||||||
:type u: typing.List[float]
|
|
||||||
"""
|
|
||||||
#? ["count"]
|
#? ["count"]
|
||||||
p.c
|
p.c
|
||||||
#? int()
|
#? int()
|
||||||
@@ -43,13 +39,8 @@ def we_can_has_sequence(p, q, r, s, t, u):
|
|||||||
#? float()
|
#? float()
|
||||||
u[1]
|
u[1]
|
||||||
|
|
||||||
def iterators(ps, qs, rs, ts):
|
def iterators(ps: Iterable[int], qs: Iterator[str], rs:
|
||||||
"""
|
Sequence["ForwardReference"], ts: AbstractSet["float"]):
|
||||||
:type ps: typing.Iterable[int]
|
|
||||||
:type qs: typing.Iterator[str]
|
|
||||||
:type rs: typing.Sequence["ForwardReference"]
|
|
||||||
:type ts: typing.AbstractSet["float"]
|
|
||||||
"""
|
|
||||||
for p in ps:
|
for p in ps:
|
||||||
#? int()
|
#? int()
|
||||||
p
|
p
|
||||||
@@ -79,22 +70,13 @@ def iterators(ps, qs, rs, ts):
|
|||||||
#? float()
|
#? float()
|
||||||
t
|
t
|
||||||
|
|
||||||
def sets(p, q):
|
def sets(p: AbstractSet[int], q: typing.MutableSet[float]):
|
||||||
"""
|
|
||||||
:type p: typing.AbstractSet[int]
|
|
||||||
:type q: typing.MutableSet[float]
|
|
||||||
"""
|
|
||||||
#? []
|
#? []
|
||||||
p.a
|
p.a
|
||||||
#? ["add"]
|
#? ["add"]
|
||||||
q.a
|
q.a
|
||||||
|
|
||||||
def tuple(p, q, r):
|
def tuple(p: Tuple[int], q: Tuple[int, str, float], r: Tuple[B, ...]):
|
||||||
"""
|
|
||||||
:type p: typing.Tuple[int]
|
|
||||||
:type q: typing.Tuple[int, str, float]
|
|
||||||
:type r: typing.Tuple[B, ...]
|
|
||||||
"""
|
|
||||||
#? int()
|
#? int()
|
||||||
p[0]
|
p[0]
|
||||||
#? ['index']
|
#? ['index']
|
||||||
@@ -127,16 +109,14 @@ class Key:
|
|||||||
class Value:
|
class Value:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def mapping(p, q, d, dd, r, s, t):
|
def mapping(
|
||||||
"""
|
p: Mapping[Key, Value],
|
||||||
:type p: typing.Mapping[Key, Value]
|
q: typing.MutableMapping[Key, Value],
|
||||||
:type q: typing.MutableMapping[Key, Value]
|
d: Dict[Key, Value],
|
||||||
:type d: typing.Dict[Key, Value]
|
dd: typing.DefaultDict[Key, Value],
|
||||||
:type dd: typing.DefaultDict[Key, Value]
|
r: typing.KeysView[Key],
|
||||||
:type r: typing.KeysView[Key]
|
s: typing.ValuesView[Value],
|
||||||
:type s: typing.ValuesView[Value]
|
t: typing.ItemsView[Key, Value]):
|
||||||
:type t: typing.ItemsView[Key, Value]
|
|
||||||
"""
|
|
||||||
#? []
|
#? []
|
||||||
p.setd
|
p.setd
|
||||||
#? ["setdefault"]
|
#? ["setdefault"]
|
||||||
@@ -198,14 +178,12 @@ def mapping(p, q, d, dd, r, s, t):
|
|||||||
#? Value()
|
#? Value()
|
||||||
value
|
value
|
||||||
|
|
||||||
def union(p, q, r, s, t):
|
def union(
|
||||||
"""
|
p: Union[int],
|
||||||
:type p: typing.Union[int]
|
q: Union[int, int],
|
||||||
:type q: typing.Union[int, int]
|
r: Union[int, str, "int"],
|
||||||
:type r: typing.Union[int, str, "int"]
|
s: Union[int, typing.Union[str, "typing.Union['float', 'dict']"]],
|
||||||
:type s: typing.Union[int, typing.Union[str, "typing.Union['float', 'dict']"]]
|
t: Union[int, None]):
|
||||||
:type t: typing.Union[int, None]
|
|
||||||
"""
|
|
||||||
#? int()
|
#? int()
|
||||||
p
|
p
|
||||||
#? int()
|
#? int()
|
||||||
@@ -217,9 +195,8 @@ def union(p, q, r, s, t):
|
|||||||
#? int() None
|
#? int() None
|
||||||
t
|
t
|
||||||
|
|
||||||
def optional(p):
|
def optional(p: Optional[int]):
|
||||||
"""
|
"""
|
||||||
:type p: typing.Optional[int]
|
|
||||||
Optional does not do anything special. However it should be recognised
|
Optional does not do anything special. However it should be recognised
|
||||||
as being of that type. Jedi doesn't do anything with the extra into that
|
as being of that type. Jedi doesn't do anything with the extra into that
|
||||||
it can be None as well
|
it can be None as well
|
||||||
@@ -234,10 +211,7 @@ class TestDict(typing.Dict[str, int]):
|
|||||||
def setdud(self):
|
def setdud(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def testdict(x):
|
def testdict(x: TestDict):
|
||||||
"""
|
|
||||||
:type x: TestDict
|
|
||||||
"""
|
|
||||||
#? ["setdud", "setdefault"]
|
#? ["setdud", "setdefault"]
|
||||||
x.setd
|
x.setd
|
||||||
for key in x.keys():
|
for key in x.keys():
|
||||||
@@ -262,10 +236,7 @@ y = WrappingType(0) # Per https://github.com/davidhalter/jedi/issues/1015#issuec
|
|||||||
#? str()
|
#? str()
|
||||||
y
|
y
|
||||||
|
|
||||||
def testnewtype(y):
|
def testnewtype(y: WrappingType):
|
||||||
"""
|
|
||||||
:type y: WrappingType
|
|
||||||
"""
|
|
||||||
#? str()
|
#? str()
|
||||||
y
|
y
|
||||||
#? ["upper"]
|
#? ["upper"]
|
||||||
@@ -273,10 +244,7 @@ def testnewtype(y):
|
|||||||
|
|
||||||
WrappingType2 = typing.NewType()
|
WrappingType2 = typing.NewType()
|
||||||
|
|
||||||
def testnewtype2(y):
|
def testnewtype2(y: WrappingType2):
|
||||||
"""
|
|
||||||
:type y: WrappingType2
|
|
||||||
"""
|
|
||||||
#?
|
#?
|
||||||
y
|
y
|
||||||
#? []
|
#? []
|
||||||
@@ -297,10 +265,7 @@ class TestDefaultDict(typing.DefaultDict[str, int]):
|
|||||||
def setdud(self):
|
def setdud(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def testdict(x):
|
def testdict(x: TestDefaultDict):
|
||||||
"""
|
|
||||||
:type x: TestDefaultDict
|
|
||||||
"""
|
|
||||||
#? ["setdud", "setdefault"]
|
#? ["setdud", "setdefault"]
|
||||||
x.setd
|
x.setd
|
||||||
for key in x.keys():
|
for key in x.keys():
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
"""
|
"""
|
||||||
PEP 526 introduced a new way of using type annotations on variables. It was
|
PEP 526 introduced a way of using type annotations on variables.
|
||||||
introduced in Python 3.6.
|
|
||||||
"""
|
"""
|
||||||
# python >= 3.6
|
|
||||||
|
|
||||||
import typing
|
import typing
|
||||||
|
|
||||||
asdf = ''
|
asdf = ''
|
||||||
@@ -47,7 +44,7 @@ class Foo():
|
|||||||
baz: typing.ClassVar[str]
|
baz: typing.ClassVar[str]
|
||||||
|
|
||||||
|
|
||||||
#?
|
#? int()
|
||||||
Foo.bar
|
Foo.bar
|
||||||
#? int()
|
#? int()
|
||||||
Foo().bar
|
Foo().bar
|
||||||
@@ -61,6 +58,7 @@ class VarClass:
|
|||||||
var_instance2: float
|
var_instance2: float
|
||||||
var_class1: typing.ClassVar[str] = 1
|
var_class1: typing.ClassVar[str] = 1
|
||||||
var_class2: typing.ClassVar[bytes]
|
var_class2: typing.ClassVar[bytes]
|
||||||
|
var_class3 = None
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
#? int()
|
#? int()
|
||||||
@@ -73,15 +71,21 @@ class VarClass:
|
|||||||
d.var_class2
|
d.var_class2
|
||||||
#? []
|
#? []
|
||||||
d.int
|
d.int
|
||||||
#? ['var_class1', 'var_class2', 'var_instance1', 'var_instance2']
|
#? ['var_class1', 'var_class2', 'var_instance1', 'var_instance2', 'var_class3']
|
||||||
self.var_
|
self.var_
|
||||||
|
|
||||||
|
class VarClass2(VarClass):
|
||||||
|
var_class3: typing.ClassVar[int]
|
||||||
|
|
||||||
#? ['var_class1', 'var_class2', 'var_instance1']
|
def __init__(self):
|
||||||
|
#? int()
|
||||||
|
self.var_class3
|
||||||
|
|
||||||
|
#? ['var_class1', 'var_class2', 'var_instance1', 'var_class3', 'var_instance2']
|
||||||
VarClass.var_
|
VarClass.var_
|
||||||
#? int()
|
#? int()
|
||||||
VarClass.var_instance1
|
VarClass.var_instance1
|
||||||
#?
|
#? float()
|
||||||
VarClass.var_instance2
|
VarClass.var_instance2
|
||||||
#? str()
|
#? str()
|
||||||
VarClass.var_class1
|
VarClass.var_class1
|
||||||
@@ -91,7 +95,7 @@ VarClass.var_class2
|
|||||||
VarClass.int
|
VarClass.int
|
||||||
|
|
||||||
d = VarClass()
|
d = VarClass()
|
||||||
#? ['var_class1', 'var_class2', 'var_instance1', 'var_instance2']
|
#? ['var_class1', 'var_class2', 'var_class3', 'var_instance1', 'var_instance2']
|
||||||
d.var_
|
d.var_
|
||||||
#? int()
|
#? int()
|
||||||
d.var_instance1
|
d.var_instance1
|
||||||
|
|||||||
26
test/completion/pep0593_annotations.py
Normal file
26
test/completion/pep0593_annotations.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
# python >= 3.9
|
||||||
|
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
|
# This is just a dummy and very meaningless thing to use with to the Annotated
|
||||||
|
# type hint
|
||||||
|
class Foo:
|
||||||
|
pass
|
||||||
|
|
||||||
|
class A:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def annotated_function_params(
|
||||||
|
basic: Annotated[str, Foo()],
|
||||||
|
obj: A,
|
||||||
|
annotated_obj: Annotated[A, Foo()],
|
||||||
|
):
|
||||||
|
#? str()
|
||||||
|
basic
|
||||||
|
|
||||||
|
#? A()
|
||||||
|
obj
|
||||||
|
|
||||||
|
#? A()
|
||||||
|
annotated_obj
|
||||||
46
test/completion/pep0604.py
Normal file
46
test/completion/pep0604.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
from pep0484_generic_parameters import list_t_to_list_t
|
||||||
|
|
||||||
|
list_of_ints_and_strs: list[int | str]
|
||||||
|
|
||||||
|
# Test that unions are handled
|
||||||
|
x2 = list_t_to_list_t(list_of_ints_and_strs)[0]
|
||||||
|
#? int() str()
|
||||||
|
x2
|
||||||
|
|
||||||
|
for z in list_t_to_list_t(list_of_ints_and_strs):
|
||||||
|
#? int() str()
|
||||||
|
z
|
||||||
|
|
||||||
|
|
||||||
|
from pep0484_generic_passthroughs import (
|
||||||
|
typed_variadic_tuple_generic_passthrough,
|
||||||
|
)
|
||||||
|
|
||||||
|
variadic_tuple_str_int: tuple[int | str, ...]
|
||||||
|
|
||||||
|
for m in typed_variadic_tuple_generic_passthrough(variadic_tuple_str_int):
|
||||||
|
#? str() int()
|
||||||
|
m
|
||||||
|
|
||||||
|
|
||||||
|
def func_returns_byteslike() -> bytes | bytearray:
|
||||||
|
pass
|
||||||
|
|
||||||
|
#? bytes() bytearray()
|
||||||
|
func_returns_byteslike()
|
||||||
|
|
||||||
|
|
||||||
|
pep604_optional_1: int | str | None
|
||||||
|
pep604_optional_2: None | bytes
|
||||||
|
|
||||||
|
#? int() str() None
|
||||||
|
pep604_optional_1
|
||||||
|
|
||||||
|
#? None bytes()
|
||||||
|
pep604_optional_2
|
||||||
|
|
||||||
|
|
||||||
|
pep604_in_str: "int | bytes"
|
||||||
|
|
||||||
|
#? int() bytes()
|
||||||
|
pep604_in_str
|
||||||
@@ -96,6 +96,9 @@ def test_x(my_con
|
|||||||
#? 18 ['my_conftest_fixture']
|
#? 18 ['my_conftest_fixture']
|
||||||
def test_x(my_conftest_fixture):
|
def test_x(my_conftest_fixture):
|
||||||
return
|
return
|
||||||
|
#? ['my_module_fixture']
|
||||||
|
def test_x(my_modu
|
||||||
|
return
|
||||||
|
|
||||||
#? []
|
#? []
|
||||||
def lala(my_con
|
def lala(my_con
|
||||||
@@ -139,9 +142,6 @@ def test_p(monkeypatch):
|
|||||||
#? ['capsysbinary']
|
#? ['capsysbinary']
|
||||||
def test_p(capsysbin
|
def test_p(capsysbin
|
||||||
|
|
||||||
#? ['tmpdir', 'tmpdir_factory']
|
|
||||||
def test_p(tmpdi
|
|
||||||
|
|
||||||
|
|
||||||
def close_parens():
|
def close_parens():
|
||||||
pass
|
pass
|
||||||
@@ -183,3 +183,28 @@ def with_annot() -> Generator[float, None, None]:
|
|||||||
def test_with_annot(inheritance_fixture, with_annot):
|
def test_with_annot(inheritance_fixture, with_annot):
|
||||||
#? float()
|
#? float()
|
||||||
with_annot
|
with_annot
|
||||||
|
|
||||||
|
# -----------------
|
||||||
|
# pytest external plugins
|
||||||
|
# -----------------
|
||||||
|
|
||||||
|
#? ['admin_user', 'admin_client']
|
||||||
|
def test_z(admin
|
||||||
|
|
||||||
|
#! 15 ['def admin_client']
|
||||||
|
def test_p(admin_client):
|
||||||
|
#? ['login', 'logout']
|
||||||
|
admin_client.log
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
@some_decorator
|
||||||
|
#? ['admin_user']
|
||||||
|
def bla(admin_u
|
||||||
|
return
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
@some_decorator
|
||||||
|
#! 12 ['def admin_user']
|
||||||
|
def bla(admin_user):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
from pytest import fixture
|
||||||
|
|
||||||
|
|
||||||
|
@fixture()
|
||||||
|
def admin_user():
|
||||||
|
pass
|
||||||
16
test/examples/pytest_plugin_package/pytest_plugin/plugin.py
Normal file
16
test/examples/pytest_plugin_package/pytest_plugin/plugin.py
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from .fixtures import admin_user # noqa
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def admin_client():
|
||||||
|
return Client()
|
||||||
|
|
||||||
|
|
||||||
|
class Client:
|
||||||
|
def login(self, **credentials):
|
||||||
|
...
|
||||||
|
|
||||||
|
def logout(self):
|
||||||
|
...
|
||||||
@@ -134,7 +134,7 @@ TEST_GOTO = 2
|
|||||||
TEST_REFERENCES = 3
|
TEST_REFERENCES = 3
|
||||||
|
|
||||||
|
|
||||||
grammar36 = parso.load_grammar(version='3.6')
|
grammar313 = parso.load_grammar(version='3.13')
|
||||||
|
|
||||||
|
|
||||||
class BaseTestCase(object):
|
class BaseTestCase(object):
|
||||||
@@ -209,7 +209,7 @@ class IntegrationTestCase(BaseTestCase):
|
|||||||
TEST_REFERENCES: self.run_get_references,
|
TEST_REFERENCES: self.run_get_references,
|
||||||
}
|
}
|
||||||
if (self.path.endswith('pytest.py') or self.path.endswith('conftest.py')) \
|
if (self.path.endswith('pytest.py') or self.path.endswith('conftest.py')) \
|
||||||
and environment.executable != os.path.realpath(sys.executable):
|
and os.path.realpath(environment.executable) != os.path.realpath(sys.executable):
|
||||||
# It's not guarantueed that pytest is installed in test
|
# It's not guarantueed that pytest is installed in test
|
||||||
# environments, if we're not running in the same environment that
|
# environments, if we're not running in the same environment that
|
||||||
# we're already in, so just skip that case.
|
# we're already in, so just skip that case.
|
||||||
@@ -238,7 +238,7 @@ class IntegrationTestCase(BaseTestCase):
|
|||||||
should_be = set()
|
should_be = set()
|
||||||
for match in re.finditer('(?:[^ ]+)', correct):
|
for match in re.finditer('(?:[^ ]+)', correct):
|
||||||
string = match.group(0)
|
string = match.group(0)
|
||||||
parser = grammar36.parse(string, start_symbol='eval_input', error_recovery=False)
|
parser = grammar313.parse(string, start_symbol='eval_input', error_recovery=False)
|
||||||
parser_utils.move(parser.get_root_node(), self.line_nr)
|
parser_utils.move(parser.get_root_node(), self.line_nr)
|
||||||
node = parser.get_root_node()
|
node = parser.get_root_node()
|
||||||
module_context = script._get_module_context()
|
module_context = script._get_module_context()
|
||||||
@@ -504,7 +504,7 @@ if __name__ == '__main__':
|
|||||||
if arguments['--env']:
|
if arguments['--env']:
|
||||||
environment = get_system_environment(arguments['--env'])
|
environment = get_system_environment(arguments['--env'])
|
||||||
else:
|
else:
|
||||||
# Will be 3.6.
|
# Will be 3.13.
|
||||||
environment = get_default_environment()
|
environment = get_default_environment()
|
||||||
|
|
||||||
import traceback
|
import traceback
|
||||||
|
|||||||
@@ -321,10 +321,19 @@ def test_docstrings_for_completions(Script):
|
|||||||
assert isinstance(c.docstring(), str)
|
assert isinstance(c.docstring(), str)
|
||||||
|
|
||||||
|
|
||||||
|
def test_completions_order_most_resemblance_on_top(Script):
|
||||||
|
"""Test that the completion which resembles the in-typing the most will come first."""
|
||||||
|
code = "from pathlib import Path\npath = Path('hello.txt')\n\npat"
|
||||||
|
script = Script(code)
|
||||||
|
# User is typing "pat" and "path" is closer to it than "Path".
|
||||||
|
assert ['path', 'Path'] == [comp.name for comp in script.complete()]
|
||||||
|
|
||||||
|
|
||||||
def test_fuzzy_completion(Script):
|
def test_fuzzy_completion(Script):
|
||||||
script = Script('string = "hello"\nstring.upper')
|
script = Script('string = "hello"\nstring.upper')
|
||||||
assert ['isupper',
|
# 'isupper' is included because it is fuzzily matched.
|
||||||
'upper'] == [comp.name for comp in script.complete(fuzzy=True)]
|
assert ['upper',
|
||||||
|
'isupper'] == [comp.name for comp in script.complete(fuzzy=True)]
|
||||||
|
|
||||||
|
|
||||||
def test_math_fuzzy_completion(Script, environment):
|
def test_math_fuzzy_completion(Script, environment):
|
||||||
|
|||||||
@@ -650,6 +650,7 @@ def test_cursor_after_signature(Script, column):
|
|||||||
('abs(chr ( \nclass y: pass', 1, 8, 'abs', 0),
|
('abs(chr ( \nclass y: pass', 1, 8, 'abs', 0),
|
||||||
('abs(chr ( \nclass y: pass', 1, 9, 'abs', 0),
|
('abs(chr ( \nclass y: pass', 1, 9, 'abs', 0),
|
||||||
('abs(chr ( \nclass y: pass', 1, 10, 'chr', 0),
|
('abs(chr ( \nclass y: pass', 1, 10, 'chr', 0),
|
||||||
|
('abs(foo.bar=3)', 1, 13, 'abs', 0),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
def test_base_signatures(Script, code, line, column, name, index):
|
def test_base_signatures(Script, code, line, column, name, index):
|
||||||
|
|||||||
@@ -188,10 +188,7 @@ def test_functions_should_have_params(Script):
|
|||||||
assert c.get_signatures()
|
assert c.get_signatures()
|
||||||
|
|
||||||
|
|
||||||
def test_hashlib_params(Script, environment):
|
def test_hashlib_params(Script):
|
||||||
if environment.version_info < (3,):
|
|
||||||
pytest.skip()
|
|
||||||
|
|
||||||
script = Script('from hashlib import sha256')
|
script = Script('from hashlib import sha256')
|
||||||
c, = script.complete()
|
c, = script.complete()
|
||||||
sig, = c.get_signatures()
|
sig, = c.get_signatures()
|
||||||
@@ -351,8 +348,8 @@ def test_parent_on_comprehension(Script):
|
|||||||
|
|
||||||
def test_type(Script):
|
def test_type(Script):
|
||||||
for c in Script('a = [str()]; a[0].').complete():
|
for c in Script('a = [str()]; a[0].').complete():
|
||||||
if c.name == '__class__' and False: # TODO fix.
|
if c.name == '__class__':
|
||||||
assert c.type == 'class'
|
assert c.type == 'property'
|
||||||
else:
|
else:
|
||||||
assert c.type in ('function', 'statement')
|
assert c.type in ('function', 'statement')
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,11 @@ def test_import_keyword(Script):
|
|||||||
# unrelated to #44
|
# unrelated to #44
|
||||||
|
|
||||||
|
|
||||||
|
def test_import_keyword_after_newline(Script):
|
||||||
|
d, = Script("import x\nimport y").help(line=2, column=0)
|
||||||
|
assert d.docstring().startswith('The "import" statement')
|
||||||
|
|
||||||
|
|
||||||
def test_import_keyword_with_gotos(goto_or_infer):
|
def test_import_keyword_with_gotos(goto_or_infer):
|
||||||
assert not goto_or_infer("import x", column=0)
|
assert not goto_or_infer("import x", column=0)
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ def test_find_system_environments():
|
|||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
'version',
|
'version',
|
||||||
['3.6', '3.7', '3.8', '3.9']
|
['3.8', '3.9', '3.10', '3.11', '3.12', '3.13']
|
||||||
)
|
)
|
||||||
def test_versions(version):
|
def test_versions(version):
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import typing
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import jedi
|
import jedi
|
||||||
|
import jedi.settings
|
||||||
from jedi.inference.compiled import mixed
|
from jedi.inference.compiled import mixed
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
|
|
||||||
@@ -25,7 +26,7 @@ def get_completion(source, namespace):
|
|||||||
|
|
||||||
|
|
||||||
def test_builtin_details():
|
def test_builtin_details():
|
||||||
import keyword
|
import keyword # noqa: F401
|
||||||
|
|
||||||
class EmptyClass:
|
class EmptyClass:
|
||||||
pass
|
pass
|
||||||
@@ -52,9 +53,9 @@ def test_numpy_like_non_zero():
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
class NumpyNonZero:
|
class NumpyNonZero:
|
||||||
|
|
||||||
def __zero__(self):
|
def __zero__(self):
|
||||||
raise ValueError('Numpy arrays would raise and tell you to use .any() or all()')
|
raise ValueError('Numpy arrays would raise and tell you to use .any() or all()')
|
||||||
|
|
||||||
def __bool__(self):
|
def __bool__(self):
|
||||||
raise ValueError('Numpy arrays would raise and tell you to use .any() or all()')
|
raise ValueError('Numpy arrays would raise and tell you to use .any() or all()')
|
||||||
|
|
||||||
@@ -101,26 +102,28 @@ def test_side_effect_completion():
|
|||||||
assert foo.name == 'foo'
|
assert foo.name == 'foo'
|
||||||
|
|
||||||
|
|
||||||
def _assert_interpreter_complete(source, namespace, completions,
|
def _assert_interpreter_complete(source, namespace, completions, *, check_type=False, **kwds):
|
||||||
**kwds):
|
|
||||||
script = jedi.Interpreter(source, [namespace], **kwds)
|
script = jedi.Interpreter(source, [namespace], **kwds)
|
||||||
cs = script.complete()
|
cs = script.complete()
|
||||||
actual = [c.name for c in cs]
|
actual = [c.name for c in cs]
|
||||||
|
if check_type:
|
||||||
|
for c in cs:
|
||||||
|
c.type
|
||||||
assert sorted(actual) == sorted(completions)
|
assert sorted(actual) == sorted(completions)
|
||||||
|
|
||||||
|
|
||||||
def test_complete_raw_function():
|
def test_complete_raw_function():
|
||||||
from os.path import join
|
from os.path import join # noqa: F401
|
||||||
_assert_interpreter_complete('join("").up', locals(), ['upper'])
|
_assert_interpreter_complete('join("").up', locals(), ['upper'])
|
||||||
|
|
||||||
|
|
||||||
def test_complete_raw_function_different_name():
|
def test_complete_raw_function_different_name():
|
||||||
from os.path import join as pjoin
|
from os.path import join as pjoin # noqa: F401
|
||||||
_assert_interpreter_complete('pjoin("").up', locals(), ['upper'])
|
_assert_interpreter_complete('pjoin("").up', locals(), ['upper'])
|
||||||
|
|
||||||
|
|
||||||
def test_complete_raw_module():
|
def test_complete_raw_module():
|
||||||
import os
|
import os # noqa: F401
|
||||||
_assert_interpreter_complete('os.path.join("a").up', locals(), ['upper'])
|
_assert_interpreter_complete('os.path.join("a").up', locals(), ['upper'])
|
||||||
|
|
||||||
|
|
||||||
@@ -219,7 +222,7 @@ def test__getattr__completions(allow_unsafe_getattr, class_is_findable):
|
|||||||
|
|
||||||
@pytest.fixture(params=[False, True])
|
@pytest.fixture(params=[False, True])
|
||||||
def allow_unsafe_getattr(request, monkeypatch):
|
def allow_unsafe_getattr(request, monkeypatch):
|
||||||
monkeypatch.setattr(jedi.Interpreter, '_allow_descriptor_getattr_default', request.param)
|
monkeypatch.setattr(jedi.settings, 'allow_unsafe_interpreter_executions', request.param)
|
||||||
return request.param
|
return request.param
|
||||||
|
|
||||||
|
|
||||||
@@ -278,7 +281,7 @@ def test_param_completion():
|
|||||||
def foo(bar):
|
def foo(bar):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
lambd = lambda xyz: 3
|
lambd = lambda xyz: 3 # noqa: E731
|
||||||
|
|
||||||
_assert_interpreter_complete('foo(bar', locals(), ['bar='])
|
_assert_interpreter_complete('foo(bar', locals(), ['bar='])
|
||||||
_assert_interpreter_complete('lambd(xyz', locals(), ['xyz='])
|
_assert_interpreter_complete('lambd(xyz', locals(), ['xyz='])
|
||||||
@@ -292,7 +295,7 @@ def test_endless_yield():
|
|||||||
|
|
||||||
|
|
||||||
def test_completion_params():
|
def test_completion_params():
|
||||||
foo = lambda a, b=3: None
|
foo = lambda a, b=3: None # noqa: E731
|
||||||
|
|
||||||
script = jedi.Interpreter('foo', [locals()])
|
script = jedi.Interpreter('foo', [locals()])
|
||||||
c, = script.complete()
|
c, = script.complete()
|
||||||
@@ -307,8 +310,9 @@ def test_completion_param_annotations():
|
|||||||
# Need to define this function not directly in Python. Otherwise Jedi is too
|
# Need to define this function not directly in Python. Otherwise Jedi is too
|
||||||
# clever and uses the Python code instead of the signature object.
|
# clever and uses the Python code instead of the signature object.
|
||||||
code = 'def foo(a: 1, b: str, c: int = 1.0) -> bytes: pass'
|
code = 'def foo(a: 1, b: str, c: int = 1.0) -> bytes: pass'
|
||||||
exec(code, locals())
|
exec_locals = {}
|
||||||
script = jedi.Interpreter('foo', [locals()])
|
exec(code, exec_locals)
|
||||||
|
script = jedi.Interpreter('foo', [exec_locals])
|
||||||
c, = script.complete()
|
c, = script.complete()
|
||||||
sig, = c.get_signatures()
|
sig, = c.get_signatures()
|
||||||
a, b, c = sig.params
|
a, b, c = sig.params
|
||||||
@@ -320,7 +324,7 @@ def test_completion_param_annotations():
|
|||||||
assert b.description == 'param b: str'
|
assert b.description == 'param b: str'
|
||||||
assert c.description == 'param c: int=1.0'
|
assert c.description == 'param c: int=1.0'
|
||||||
|
|
||||||
d, = jedi.Interpreter('foo()', [locals()]).infer()
|
d, = jedi.Interpreter('foo()', [exec_locals]).infer()
|
||||||
assert d.name == 'bytes'
|
assert d.name == 'bytes'
|
||||||
|
|
||||||
|
|
||||||
@@ -406,7 +410,7 @@ def test_dir_magic_method(allow_unsafe_getattr):
|
|||||||
def test_name_not_findable():
|
def test_name_not_findable():
|
||||||
class X():
|
class X():
|
||||||
if 0:
|
if 0:
|
||||||
NOT_FINDABLE
|
NOT_FINDABLE # noqa: F821
|
||||||
|
|
||||||
def hidden(self):
|
def hidden(self):
|
||||||
return
|
return
|
||||||
@@ -419,7 +423,7 @@ def test_name_not_findable():
|
|||||||
|
|
||||||
|
|
||||||
def test_stubs_working():
|
def test_stubs_working():
|
||||||
from multiprocessing import cpu_count
|
from multiprocessing import cpu_count # noqa: F401
|
||||||
defs = jedi.Interpreter("cpu_count()", [locals()]).infer()
|
defs = jedi.Interpreter("cpu_count()", [locals()]).infer()
|
||||||
assert [d.name for d in defs] == ['int']
|
assert [d.name for d in defs] == ['int']
|
||||||
|
|
||||||
@@ -522,14 +526,17 @@ def test_partial_signatures(code, expected, index):
|
|||||||
c = functools.partial(func, 1, c=2)
|
c = functools.partial(func, 1, c=2)
|
||||||
|
|
||||||
sig, = jedi.Interpreter(code, [locals()]).get_signatures()
|
sig, = jedi.Interpreter(code, [locals()]).get_signatures()
|
||||||
assert sig.name == 'partial'
|
|
||||||
assert [p.name for p in sig.params] == expected
|
assert [p.name for p in sig.params] == expected
|
||||||
assert index == sig.index
|
assert index == sig.index
|
||||||
|
|
||||||
|
if sys.version_info < (3, 13):
|
||||||
|
# Python 3.13.0b3 makes functools.partial be a descriptor, which breaks
|
||||||
|
# Jedi's `py__name__` detection; see https://github.com/davidhalter/jedi/issues/2012
|
||||||
|
assert sig.name == 'partial'
|
||||||
|
|
||||||
|
|
||||||
def test_type_var():
|
def test_type_var():
|
||||||
"""This was an issue before, see Github #1369"""
|
"""This was an issue before, see Github #1369"""
|
||||||
import typing
|
|
||||||
x = typing.TypeVar('myvar')
|
x = typing.TypeVar('myvar')
|
||||||
def_, = jedi.Interpreter('x', [locals()]).infer()
|
def_, = jedi.Interpreter('x', [locals()]).infer()
|
||||||
assert def_.name == 'TypeVar'
|
assert def_.name == 'TypeVar'
|
||||||
@@ -607,15 +614,15 @@ def test_dict_getitem(code, types):
|
|||||||
('next(DunderCls())', 'float'),
|
('next(DunderCls())', 'float'),
|
||||||
('next(dunder)', 'float'),
|
('next(dunder)', 'float'),
|
||||||
('for x in DunderCls(): x', 'str'),
|
('for x in DunderCls(): x', 'str'),
|
||||||
#('for x in dunder: x', 'str'),
|
# ('for x in dunder: x', 'str'),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
def test_dunders(class_is_findable, code, expected):
|
def test_dunders(class_is_findable, code, expected, allow_unsafe_getattr):
|
||||||
from typing import Iterator
|
from typing import Iterator
|
||||||
|
|
||||||
class DunderCls:
|
class DunderCls:
|
||||||
def __getitem__(self, key) -> int:
|
def __getitem__(self, key) -> int:
|
||||||
pass
|
return 1
|
||||||
|
|
||||||
def __iter__(self, key) -> Iterator[str]:
|
def __iter__(self, key) -> Iterator[str]:
|
||||||
pass
|
pass
|
||||||
@@ -656,10 +663,12 @@ def bar():
|
|||||||
({'return': 'typing.Union[str, int]'}, ['int', 'str'], ''),
|
({'return': 'typing.Union[str, int]'}, ['int', 'str'], ''),
|
||||||
({'return': 'typing.Union["str", int]'},
|
({'return': 'typing.Union["str", int]'},
|
||||||
['int', 'str'] if sys.version_info >= (3, 9) else ['int'], ''),
|
['int', 'str'] if sys.version_info >= (3, 9) else ['int'], ''),
|
||||||
({'return': 'typing.Union["str", 1]'}, [], ''),
|
({'return': 'typing.Union["str", 1]'},
|
||||||
|
['str'] if sys.version_info >= (3, 11) else [], ''),
|
||||||
({'return': 'typing.Optional[str]'}, ['NoneType', 'str'], ''),
|
({'return': 'typing.Optional[str]'}, ['NoneType', 'str'], ''),
|
||||||
({'return': 'typing.Optional[str, int]'}, [], ''), # Takes only one arg
|
({'return': 'typing.Optional[str, int]'}, [], ''), # Takes only one arg
|
||||||
({'return': 'typing.Any'}, [], ''),
|
({'return': 'typing.Any'},
|
||||||
|
['_AnyMeta'] if sys.version_info >= (3, 11) else [], ''),
|
||||||
|
|
||||||
({'return': 'typing.Tuple[int, str]'},
|
({'return': 'typing.Tuple[int, str]'},
|
||||||
['Tuple' if sys.version_info[:2] == (3, 6) else 'tuple'], ''),
|
['Tuple' if sys.version_info[:2] == (3, 6) else 'tuple'], ''),
|
||||||
@@ -682,7 +691,7 @@ def bar():
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
def test_string_annotation(annotations, result, code):
|
def test_string_annotation(annotations, result, code):
|
||||||
x = lambda foo: 1
|
x = lambda foo: 1 # noqa: E731
|
||||||
x.__annotations__ = annotations
|
x.__annotations__ = annotations
|
||||||
defs = jedi.Interpreter(code or 'x()', [locals()]).infer()
|
defs = jedi.Interpreter(code or 'x()', [locals()]).infer()
|
||||||
assert [d.name for d in defs] == result
|
assert [d.name for d in defs] == result
|
||||||
@@ -715,8 +724,8 @@ def test_negate():
|
|||||||
|
|
||||||
def test_complete_not_findable_class_source():
|
def test_complete_not_findable_class_source():
|
||||||
class TestClass():
|
class TestClass():
|
||||||
ta=1
|
ta = 1
|
||||||
ta1=2
|
ta1 = 2
|
||||||
|
|
||||||
# Simulate the environment where the class is defined in
|
# Simulate the environment where the class is defined in
|
||||||
# an interactive session and therefore inspect module
|
# an interactive session and therefore inspect module
|
||||||
@@ -739,3 +748,114 @@ def test_param_infer_default():
|
|||||||
param, = abs_sig.params
|
param, = abs_sig.params
|
||||||
assert param.name == 'x'
|
assert param.name == 'x'
|
||||||
assert param.infer_default() == []
|
assert param.infer_default() == []
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'code, expected', [
|
||||||
|
("random.triangular(", ['high=', 'low=', 'mode=']),
|
||||||
|
("random.triangular(low=1, ", ['high=', 'mode=']),
|
||||||
|
("random.triangular(high=1, ", ['low=', 'mode=']),
|
||||||
|
("random.triangular(low=1, high=2, ", ['mode=']),
|
||||||
|
("random.triangular(low=1, mode=2, ", ['high=']),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_keyword_param_completion(code, expected):
|
||||||
|
import random # noqa: F401
|
||||||
|
completions = jedi.Interpreter(code, [locals()]).complete()
|
||||||
|
assert expected == [c.name for c in completions if c.name.endswith('=')]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('class_is_findable', [False, True])
|
||||||
|
def test_avoid_descriptor_executions_if_not_necessary(class_is_findable):
|
||||||
|
counter = 0
|
||||||
|
|
||||||
|
class AvoidDescriptor(object):
|
||||||
|
@property
|
||||||
|
def prop(self):
|
||||||
|
nonlocal counter
|
||||||
|
counter += 1
|
||||||
|
return self
|
||||||
|
|
||||||
|
if not class_is_findable:
|
||||||
|
AvoidDescriptor.__name__ = "something_somewhere"
|
||||||
|
namespace = {'b': AvoidDescriptor()}
|
||||||
|
expected = ['prop']
|
||||||
|
_assert_interpreter_complete('b.pro', namespace, expected, check_type=True)
|
||||||
|
assert counter == 0
|
||||||
|
_assert_interpreter_complete('b.prop.pro', namespace, expected, check_type=True)
|
||||||
|
assert counter == 1
|
||||||
|
|
||||||
|
|
||||||
|
class Hello:
|
||||||
|
its_me = 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('class_is_findable', [False, True])
|
||||||
|
def test_try_to_use_return_annotation_for_property(class_is_findable):
|
||||||
|
class WithProperties(object):
|
||||||
|
@property
|
||||||
|
def with_annotation1(self) -> str:
|
||||||
|
raise BaseException
|
||||||
|
|
||||||
|
@property
|
||||||
|
def with_annotation2(self) -> 'str':
|
||||||
|
raise BaseException
|
||||||
|
|
||||||
|
@property
|
||||||
|
def with_annotation3(self) -> Hello:
|
||||||
|
raise BaseException
|
||||||
|
|
||||||
|
@property
|
||||||
|
def with_annotation4(self) -> 'Hello':
|
||||||
|
raise BaseException
|
||||||
|
|
||||||
|
@property
|
||||||
|
def with_annotation_garbage1(self) -> 'asldjflksjdfljdslkjfsl': # noqa
|
||||||
|
return Hello()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def with_annotation_garbage2(self) -> 'sdf$@@$5*+8': # noqa
|
||||||
|
return Hello()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def without_annotation(self):
|
||||||
|
return ""
|
||||||
|
|
||||||
|
if not class_is_findable:
|
||||||
|
WithProperties.__name__ = "something_somewhere"
|
||||||
|
Hello.__name__ = "something_somewhere_else"
|
||||||
|
|
||||||
|
namespace = {'p': WithProperties()}
|
||||||
|
_assert_interpreter_complete('p.without_annotation.upp', namespace, ['upper'])
|
||||||
|
_assert_interpreter_complete('p.with_annotation1.upp', namespace, ['upper'])
|
||||||
|
_assert_interpreter_complete('p.with_annotation2.upp', namespace, ['upper'])
|
||||||
|
_assert_interpreter_complete('p.with_annotation3.its', namespace, ['its_me'])
|
||||||
|
_assert_interpreter_complete('p.with_annotation4.its', namespace, ['its_me'])
|
||||||
|
# This is a fallback, if the annotations don't help
|
||||||
|
_assert_interpreter_complete('p.with_annotation_garbage1.its', namespace, ['its_me'])
|
||||||
|
_assert_interpreter_complete('p.with_annotation_garbage2.its', namespace, ['its_me'])
|
||||||
|
|
||||||
|
|
||||||
|
def test_nested__getitem__():
|
||||||
|
d = {'foo': {'bar': 1}}
|
||||||
|
_assert_interpreter_complete('d["fo', locals(), ['"foo"'])
|
||||||
|
_assert_interpreter_complete('d["foo"]["ba', locals(), ['"bar"'])
|
||||||
|
_assert_interpreter_complete('(d["foo"])["ba', locals(), ['"bar"'])
|
||||||
|
_assert_interpreter_complete('((d["foo"]))["ba', locals(), ['"bar"'])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('class_is_findable', [False, True])
|
||||||
|
def test_custom__getitem__(class_is_findable, allow_unsafe_getattr):
|
||||||
|
class CustomGetItem:
|
||||||
|
def __getitem__(self, x: int):
|
||||||
|
return "asdf"
|
||||||
|
|
||||||
|
if not class_is_findable:
|
||||||
|
CustomGetItem.__name__ = "something_somewhere"
|
||||||
|
|
||||||
|
namespace = {'c': CustomGetItem()}
|
||||||
|
if not class_is_findable and not allow_unsafe_getattr:
|
||||||
|
expected = []
|
||||||
|
else:
|
||||||
|
expected = ['upper']
|
||||||
|
_assert_interpreter_complete('c["a"].up', namespace, expected)
|
||||||
|
|||||||
@@ -144,13 +144,17 @@ def test_load_save_project(tmpdir):
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
def test_search(string, full_names, kwargs):
|
def test_search(string, full_names, kwargs):
|
||||||
some_search_test_var = 1.0
|
some_search_test_var = 1.0 # noqa: F841
|
||||||
project = Project(test_dir)
|
project = Project(test_dir)
|
||||||
if kwargs.pop('complete', False) is True:
|
if kwargs.pop('complete', False) is True:
|
||||||
defs = project.complete_search(string, **kwargs)
|
defs = project.complete_search(string, **kwargs)
|
||||||
else:
|
else:
|
||||||
defs = project.search(string, **kwargs)
|
defs = project.search(string, **kwargs)
|
||||||
assert sorted([('stub:' if d.is_stub() else '') + (d.full_name or d.name) for d in defs]) == full_names
|
actual = sorted([
|
||||||
|
('stub:' if d.is_stub() else '') + (d.full_name or d.name)
|
||||||
|
for d in defs
|
||||||
|
])
|
||||||
|
assert actual == full_names
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
@@ -169,8 +173,8 @@ def test_complete_search(Script, string, completions, all_scopes):
|
|||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
'path,expected', [
|
'path,expected', [
|
||||||
(Path(__file__).parents[2], True), # The path of the project
|
(Path(__file__).parents[2], True), # The path of the project
|
||||||
(Path(__file__).parents[1], False), # The path of the tests, not a project
|
(Path(__file__).parents[1], False), # The path of the tests, not a project
|
||||||
(Path.home(), None)
|
(Path.home(), None)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
@@ -178,7 +182,7 @@ def test_is_potential_project(path, expected):
|
|||||||
|
|
||||||
if expected is None:
|
if expected is None:
|
||||||
try:
|
try:
|
||||||
expected = _CONTAINS_POTENTIAL_PROJECT in os.listdir(path)
|
expected = bool(set(_CONTAINS_POTENTIAL_PROJECT) & set(os.listdir(path)))
|
||||||
except OSError:
|
except OSError:
|
||||||
expected = False
|
expected = False
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import platform
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import jedi
|
import jedi
|
||||||
|
from test.helpers import get_example_dir
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
@@ -51,6 +54,47 @@ def test_rename_mod(Script, dir_with_content):
|
|||||||
''').format(dir=dir_with_content)
|
''').format(dir=dir_with_content)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif('sys.version_info[:2] < (3, 8)', message="Python 3.8 introduces dirs_exist_ok")
|
||||||
|
def test_namespace_package(Script, tmpdir):
|
||||||
|
origin = get_example_dir('implicit_namespace_package')
|
||||||
|
shutil.copytree(origin, tmpdir.strpath, dirs_exist_ok=True)
|
||||||
|
sys_path = [
|
||||||
|
os.path.join(tmpdir.strpath, 'ns1'),
|
||||||
|
os.path.join(tmpdir.strpath, 'ns2')
|
||||||
|
]
|
||||||
|
script_path = os.path.join(tmpdir.strpath, 'script.py')
|
||||||
|
script = Script(
|
||||||
|
'import pkg\n',
|
||||||
|
path=script_path,
|
||||||
|
project=jedi.Project(os.path.join(tmpdir.strpath, 'does-not-exist'), sys_path=sys_path),
|
||||||
|
)
|
||||||
|
refactoring = script.rename(line=1, new_name='new_pkg')
|
||||||
|
refactoring.apply()
|
||||||
|
old1 = os.path.join(sys_path[0], "pkg")
|
||||||
|
new1 = os.path.join(sys_path[0], "new_pkg")
|
||||||
|
old2 = os.path.join(sys_path[1], "pkg")
|
||||||
|
new2 = os.path.join(sys_path[1], "new_pkg")
|
||||||
|
assert not os.path.exists(old1)
|
||||||
|
assert os.path.exists(new1)
|
||||||
|
assert not os.path.exists(old2)
|
||||||
|
assert os.path.exists(new2)
|
||||||
|
|
||||||
|
changed, = iter(refactoring.get_changed_files().values())
|
||||||
|
assert changed.get_new_code() == "import new_pkg\n"
|
||||||
|
|
||||||
|
assert refactoring.get_diff() == dedent(f'''\
|
||||||
|
rename from {old1}
|
||||||
|
rename to {new1}
|
||||||
|
rename from {old2}
|
||||||
|
rename to {new2}
|
||||||
|
--- {script_path}
|
||||||
|
+++ {script_path}
|
||||||
|
@@ -1,2 +1,2 @@
|
||||||
|
-import pkg
|
||||||
|
+import new_pkg
|
||||||
|
''').format(dir=dir_with_content)
|
||||||
|
|
||||||
|
|
||||||
def test_rename_none_path(Script):
|
def test_rename_none_path(Script):
|
||||||
refactoring = Script('foo', path=None).rename(new_name='bar')
|
refactoring = Script('foo', path=None).rename(new_name='bar')
|
||||||
with pytest.raises(jedi.RefactoringError, match='on a Script with path=None'):
|
with pytest.raises(jedi.RefactoringError, match='on a Script with path=None'):
|
||||||
@@ -69,4 +113,24 @@ def test_diff_without_ending_newline(Script):
|
|||||||
b
|
b
|
||||||
-a
|
-a
|
||||||
+c
|
+c
|
||||||
|
''') # noqa: W291
|
||||||
|
|
||||||
|
|
||||||
|
def test_diff_path_outside_of_project(Script):
|
||||||
|
if platform.system().lower() == 'windows':
|
||||||
|
abs_path = r'D:\unknown_dir\file.py'
|
||||||
|
else:
|
||||||
|
abs_path = '/unknown_dir/file.py'
|
||||||
|
script = Script(
|
||||||
|
code='foo = 1',
|
||||||
|
path=abs_path,
|
||||||
|
project=jedi.get_default_project()
|
||||||
|
)
|
||||||
|
diff = script.rename(line=1, column=0, new_name='bar').get_diff()
|
||||||
|
assert diff == dedent(f'''\
|
||||||
|
--- {abs_path}
|
||||||
|
+++ {abs_path}
|
||||||
|
@@ -1 +1 @@
|
||||||
|
-foo = 1
|
||||||
|
+bar = 1
|
||||||
''')
|
''')
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys # noqa: F401
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import jedi
|
import jedi
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
|
|
||||||
|
|
||||||
def test_simple():
|
def test_simple():
|
||||||
jedi.set_debug_function()
|
jedi.set_debug_function()
|
||||||
debug.speed('foo')
|
debug.speed('foo')
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
|
import sys
|
||||||
import math
|
import math
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
import jedi
|
||||||
from jedi.inference import compiled
|
from jedi.inference import compiled
|
||||||
from jedi.inference.compiled.access import DirectObjectAccess
|
from jedi.inference.compiled.access import DirectObjectAccess
|
||||||
from jedi.inference.gradual.conversion import _stub_to_python_value_set
|
from jedi.inference.gradual.conversion import _stub_to_python_value_set
|
||||||
@@ -26,7 +28,10 @@ def test_builtin_loading(inference_state):
|
|||||||
assert not from_name.py__doc__() # It's a stub
|
assert not from_name.py__doc__() # It's a stub
|
||||||
|
|
||||||
|
|
||||||
def test_next_docstr(inference_state):
|
def test_next_docstr(inference_state, environment):
|
||||||
|
if environment.version_info[:2] != sys.version_info[:2]:
|
||||||
|
pytest.skip()
|
||||||
|
|
||||||
next_ = compiled.builtin_from_name(inference_state, 'next')
|
next_ = compiled.builtin_from_name(inference_state, 'next')
|
||||||
assert next_.tree_node is not None
|
assert next_.tree_node is not None
|
||||||
assert next_.py__doc__() == '' # It's a stub
|
assert next_.py__doc__() == '' # It's a stub
|
||||||
@@ -77,9 +82,9 @@ def test_method_completion(Script, environment):
|
|||||||
assert [c.name for c in Script(code).complete()] == ['__func__']
|
assert [c.name for c in Script(code).complete()] == ['__func__']
|
||||||
|
|
||||||
|
|
||||||
def test_time_docstring(Script):
|
def test_time_docstring():
|
||||||
import time
|
import time
|
||||||
comp, = Script('import time\ntime.sleep').complete()
|
comp, = jedi.Script('import time\ntime.sleep').complete()
|
||||||
assert comp.docstring(raw=True) == time.sleep.__doc__
|
assert comp.docstring(raw=True) == time.sleep.__doc__
|
||||||
expected = 'sleep(secs: float) -> None\n\n' + time.sleep.__doc__
|
expected = 'sleep(secs: float) -> None\n\n' + time.sleep.__doc__
|
||||||
assert comp.docstring() == expected
|
assert comp.docstring() == expected
|
||||||
|
|||||||
@@ -206,6 +206,7 @@ def test_numpydoc_parameters_set_of_values():
|
|||||||
assert 'capitalize' in names
|
assert 'capitalize' in names
|
||||||
assert 'numerator' in names
|
assert 'numerator' in names
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skipif(numpydoc_unavailable,
|
@pytest.mark.skipif(numpydoc_unavailable,
|
||||||
reason='numpydoc module is unavailable')
|
reason='numpydoc module is unavailable')
|
||||||
def test_numpydoc_parameters_set_single_value():
|
def test_numpydoc_parameters_set_single_value():
|
||||||
@@ -390,7 +391,8 @@ def test_numpydoc_yields():
|
|||||||
@pytest.mark.skipif(numpydoc_unavailable or numpy_unavailable,
|
@pytest.mark.skipif(numpydoc_unavailable or numpy_unavailable,
|
||||||
reason='numpydoc or numpy module is unavailable')
|
reason='numpydoc or numpy module is unavailable')
|
||||||
def test_numpy_returns():
|
def test_numpy_returns():
|
||||||
s = dedent('''
|
s = dedent(
|
||||||
|
'''
|
||||||
import numpy
|
import numpy
|
||||||
x = numpy.asarray([])
|
x = numpy.asarray([])
|
||||||
x.d'''
|
x.d'''
|
||||||
@@ -402,7 +404,8 @@ def test_numpy_returns():
|
|||||||
@pytest.mark.skipif(numpydoc_unavailable or numpy_unavailable,
|
@pytest.mark.skipif(numpydoc_unavailable or numpy_unavailable,
|
||||||
reason='numpydoc or numpy module is unavailable')
|
reason='numpydoc or numpy module is unavailable')
|
||||||
def test_numpy_comp_returns():
|
def test_numpy_comp_returns():
|
||||||
s = dedent('''
|
s = dedent(
|
||||||
|
'''
|
||||||
import numpy
|
import numpy
|
||||||
x = numpy.array([])
|
x = numpy.array([])
|
||||||
x.d'''
|
x.d'''
|
||||||
|
|||||||
@@ -33,7 +33,9 @@ def test_get_signatures_stdlib(Script):
|
|||||||
|
|
||||||
# Check only on linux 64 bit platform and Python3.8.
|
# Check only on linux 64 bit platform and Python3.8.
|
||||||
@pytest.mark.parametrize('load_unsafe_extensions', [False, True])
|
@pytest.mark.parametrize('load_unsafe_extensions', [False, True])
|
||||||
@pytest.mark.skipif('sys.platform != "linux" or sys.maxsize <= 2**32 or sys.version_info[:2] != (3, 8)')
|
@pytest.mark.skipif(
|
||||||
|
'sys.platform != "linux" or sys.maxsize <= 2**32 or sys.version_info[:2] != (3, 8)',
|
||||||
|
)
|
||||||
def test_init_extension_module(Script, load_unsafe_extensions):
|
def test_init_extension_module(Script, load_unsafe_extensions):
|
||||||
"""
|
"""
|
||||||
``__init__`` extension modules are also packages and Jedi should understand
|
``__init__`` extension modules are also packages and Jedi should understand
|
||||||
|
|||||||
@@ -1,13 +1,6 @@
|
|||||||
import pytest
|
|
||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
|
||||||
def skip_not_supported(environment):
|
|
||||||
if environment.version_info < (3, 6):
|
|
||||||
pytest.skip()
|
|
||||||
|
|
||||||
|
|
||||||
def test_fstring_multiline(Script):
|
def test_fstring_multiline(Script):
|
||||||
code = dedent("""\
|
code = dedent("""\
|
||||||
'' f'''s{
|
'' f'''s{
|
||||||
|
|||||||
@@ -222,7 +222,7 @@ def test_goto_stubs_on_itself(Script, code, type_):
|
|||||||
|
|
||||||
def test_module_exists_only_as_stub(Script):
|
def test_module_exists_only_as_stub(Script):
|
||||||
try:
|
try:
|
||||||
import redis
|
import redis # noqa: F401
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
@@ -234,7 +234,7 @@ def test_module_exists_only_as_stub(Script):
|
|||||||
|
|
||||||
def test_django_exists_only_as_stub(Script):
|
def test_django_exists_only_as_stub(Script):
|
||||||
try:
|
try:
|
||||||
import django
|
import django # noqa: F401
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -43,6 +43,9 @@ def test_implicit_namespace_package(Script):
|
|||||||
solution = "foo = '%s'" % solution
|
solution = "foo = '%s'" % solution
|
||||||
assert completion.description == solution
|
assert completion.description == solution
|
||||||
|
|
||||||
|
c, = script_with_path('import pkg').complete()
|
||||||
|
assert c.docstring() == ""
|
||||||
|
|
||||||
|
|
||||||
def test_implicit_nested_namespace_package(Script):
|
def test_implicit_nested_namespace_package(Script):
|
||||||
code = 'from implicit_nested_namespaces.namespace.pkg.module import CONST'
|
code = 'from implicit_nested_namespaces.namespace.pkg.module import CONST'
|
||||||
|
|||||||
@@ -297,7 +297,6 @@ def test_os_issues(Script):
|
|||||||
# Github issue #759
|
# Github issue #759
|
||||||
s = 'import os, s'
|
s = 'import os, s'
|
||||||
assert 'sys' in import_names(s)
|
assert 'sys' in import_names(s)
|
||||||
assert 'path' not in import_names(s, column=len(s) - 1)
|
|
||||||
assert 'os' in import_names(s, column=len(s) - 3)
|
assert 'os' in import_names(s, column=len(s) - 3)
|
||||||
|
|
||||||
# Some more checks
|
# Some more checks
|
||||||
@@ -308,6 +307,33 @@ def test_os_issues(Script):
|
|||||||
assert 'path' in import_names(s, column=len(s) - 3)
|
assert 'path' in import_names(s, column=len(s) - 3)
|
||||||
|
|
||||||
|
|
||||||
|
def test_duplicated_import(Script):
|
||||||
|
def import_names(*args, **kwargs):
|
||||||
|
return [d.name for d in Script(*args).complete(**kwargs)]
|
||||||
|
|
||||||
|
s = 'import os, o'
|
||||||
|
assert 'os' not in import_names(s)
|
||||||
|
assert 'os' in import_names(s, column=len(s) - 3)
|
||||||
|
|
||||||
|
s = 'from os import path, p'
|
||||||
|
assert 'path' not in import_names(s)
|
||||||
|
assert 'path' in import_names(s, column=len(s) - 3)
|
||||||
|
assert 'path' in import_names("from os import path")
|
||||||
|
assert 'path' in import_names("from os import chdir, path")
|
||||||
|
|
||||||
|
s = 'import math as mm, m'
|
||||||
|
assert 'math' not in import_names(s)
|
||||||
|
|
||||||
|
s = 'import math as os, o'
|
||||||
|
assert 'os' in import_names(s)
|
||||||
|
|
||||||
|
s = 'from os import path as pp, p'
|
||||||
|
assert 'path' not in import_names(s)
|
||||||
|
|
||||||
|
s = 'from os import chdir as path, p'
|
||||||
|
assert 'path' in import_names(s)
|
||||||
|
|
||||||
|
|
||||||
def test_path_issues(Script):
|
def test_path_issues(Script):
|
||||||
"""
|
"""
|
||||||
See pull request #684 for details.
|
See pull request #684 for details.
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import pytest
|
|
||||||
from jedi.inference.value import TreeInstance
|
from jedi.inference.value import TreeInstance
|
||||||
|
|
||||||
|
|
||||||
@@ -12,29 +11,23 @@ def _infer_literal(Script, code, is_fstring=False):
|
|||||||
return def_._name._value.get_safe_value()
|
return def_._name._value.get_safe_value()
|
||||||
|
|
||||||
|
|
||||||
def test_f_strings(Script, environment):
|
def test_f_strings(Script):
|
||||||
"""
|
"""
|
||||||
f literals are not really supported in Jedi. They just get ignored and an
|
f literals are not really supported in Jedi. They just get ignored and an
|
||||||
empty string is returned.
|
empty string is returned.
|
||||||
"""
|
"""
|
||||||
if environment.version_info < (3, 6):
|
|
||||||
pytest.skip()
|
|
||||||
|
|
||||||
assert _infer_literal(Script, 'f"asdf"', is_fstring=True) == ''
|
assert _infer_literal(Script, 'f"asdf"', is_fstring=True) == ''
|
||||||
assert _infer_literal(Script, 'f"{asdf} "', is_fstring=True) == ''
|
assert _infer_literal(Script, 'f"{asdf} "', is_fstring=True) == ''
|
||||||
assert _infer_literal(Script, 'F"{asdf} "', is_fstring=True) == ''
|
assert _infer_literal(Script, 'F"{asdf} "', is_fstring=True) == ''
|
||||||
assert _infer_literal(Script, 'rF"{asdf} "', is_fstring=True) == ''
|
assert _infer_literal(Script, 'rF"{asdf} "', is_fstring=True) == ''
|
||||||
|
|
||||||
|
|
||||||
def test_rb_strings(Script, environment):
|
def test_rb_strings(Script):
|
||||||
assert _infer_literal(Script, 'x = br"asdf"; x') == b'asdf'
|
assert _infer_literal(Script, 'x = br"asdf"; x') == b'asdf'
|
||||||
assert _infer_literal(Script, 'x = rb"asdf"; x') == b'asdf'
|
assert _infer_literal(Script, 'x = rb"asdf"; x') == b'asdf'
|
||||||
|
|
||||||
|
|
||||||
def test_thousand_separators(Script, environment):
|
def test_thousand_separators(Script):
|
||||||
if environment.version_info < (3, 6):
|
|
||||||
pytest.skip()
|
|
||||||
|
|
||||||
assert _infer_literal(Script, '1_2_3') == 123
|
assert _infer_literal(Script, '1_2_3') == 123
|
||||||
assert _infer_literal(Script, '123_456_789') == 123456789
|
assert _infer_literal(Script, '123_456_789') == 123456789
|
||||||
assert _infer_literal(Script, '0x3_4') == 52
|
assert _infer_literal(Script, '0x3_4') == 52
|
||||||
|
|||||||
@@ -16,13 +16,13 @@ def test_on_code():
|
|||||||
assert i.infer()
|
assert i.infer()
|
||||||
|
|
||||||
|
|
||||||
def test_generics_without_definition():
|
def test_generics_without_definition() -> None:
|
||||||
# Used to raise a recursion error
|
# Used to raise a recursion error
|
||||||
T = TypeVar('T')
|
T = TypeVar('T')
|
||||||
|
|
||||||
class Stack(Generic[T]):
|
class Stack(Generic[T]):
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
self.items = [] # type: List[T]
|
self.items: List[T] = []
|
||||||
|
|
||||||
def push(self, item):
|
def push(self, item):
|
||||||
self.items.append(item)
|
self.items.append(item)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
from operator import ge, lt
|
from operator import eq, ge, lt
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@@ -13,18 +13,16 @@ from ..helpers import get_example_dir
|
|||||||
'code, sig, names, op, version', [
|
'code, sig, names, op, version', [
|
||||||
('import math; math.cos', 'cos(x, /)', ['x'], ge, (3, 6)),
|
('import math; math.cos', 'cos(x, /)', ['x'], ge, (3, 6)),
|
||||||
|
|
||||||
('next', 'next(iterator, default=None, /)', ['iterator', 'default'], ge, (3, 6)),
|
('next', 'next(iterator, default=None, /)', ['iterator', 'default'], lt, (3, 12)),
|
||||||
|
('next', 'next()', [], eq, (3, 12)),
|
||||||
|
('next', 'next(iterator, default=None, /)', ['iterator', 'default'], ge, (3, 13)),
|
||||||
|
|
||||||
('str', "str(object='', /) -> str", ['object'], ge, (3, 6)),
|
('str', "str(object='', /) -> str", ['object'], ge, (3, 6)),
|
||||||
|
|
||||||
('pow', 'pow(x, y, z=None, /) -> number', ['x', 'y', 'z'], lt, (3, 6)),
|
|
||||||
('pow', 'pow(base, exp, mod=None)', ['base', 'exp', 'mod'], ge, (3, 8)),
|
('pow', 'pow(base, exp, mod=None)', ['base', 'exp', 'mod'], ge, (3, 8)),
|
||||||
|
|
||||||
('bytes.partition', 'partition(self, sep, /) -> (head, sep, tail)',
|
|
||||||
['self', 'sep'], lt, (3, 6)),
|
|
||||||
('bytes.partition', 'partition(self, sep, /)', ['self', 'sep'], ge, (3, 6)),
|
('bytes.partition', 'partition(self, sep, /)', ['self', 'sep'], ge, (3, 6)),
|
||||||
|
|
||||||
('bytes().partition', 'partition(sep, /) -> (head, sep, tail)', ['sep'], lt, (3, 6)),
|
|
||||||
('bytes().partition', 'partition(sep, /)', ['sep'], ge, (3, 6)),
|
('bytes().partition', 'partition(sep, /)', ['sep'], ge, (3, 6)),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
@@ -320,24 +318,538 @@ def test_wraps_signature(Script, code, signature):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
'start, start_params', [
|
"start, start_params, include_params",
|
||||||
['@dataclass\nclass X:', []],
|
[
|
||||||
['@dataclass(eq=True)\nclass X:', []],
|
["@dataclass\nclass X:", [], True],
|
||||||
[dedent('''
|
["@dataclass(eq=True)\nclass X:", [], True],
|
||||||
|
[
|
||||||
|
dedent(
|
||||||
|
"""
|
||||||
class Y():
|
class Y():
|
||||||
y: int
|
y: int
|
||||||
@dataclass
|
@dataclass
|
||||||
class X(Y):'''), []],
|
class X(Y):"""
|
||||||
[dedent('''
|
),
|
||||||
|
[],
|
||||||
|
True,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
dedent(
|
||||||
|
"""
|
||||||
@dataclass
|
@dataclass
|
||||||
class Y():
|
class Y():
|
||||||
y: int
|
y: int
|
||||||
z = 5
|
z = 5
|
||||||
@dataclass
|
@dataclass
|
||||||
class X(Y):'''), ['y']],
|
class X(Y):"""
|
||||||
]
|
),
|
||||||
|
["y"],
|
||||||
|
True,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
dedent(
|
||||||
|
"""
|
||||||
|
@dataclass
|
||||||
|
class Y():
|
||||||
|
y: int
|
||||||
|
class Z(Y): # Not included
|
||||||
|
z = 5
|
||||||
|
@dataclass
|
||||||
|
class X(Z):"""
|
||||||
|
),
|
||||||
|
["y"],
|
||||||
|
True,
|
||||||
|
],
|
||||||
|
# init=False
|
||||||
|
[
|
||||||
|
dedent(
|
||||||
|
"""
|
||||||
|
@dataclass(init=False)
|
||||||
|
class X:"""
|
||||||
|
),
|
||||||
|
[],
|
||||||
|
False,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
dedent(
|
||||||
|
"""
|
||||||
|
@dataclass(eq=True, init=False)
|
||||||
|
class X:"""
|
||||||
|
),
|
||||||
|
[],
|
||||||
|
False,
|
||||||
|
],
|
||||||
|
# custom init
|
||||||
|
[
|
||||||
|
dedent(
|
||||||
|
"""
|
||||||
|
@dataclass()
|
||||||
|
class X:
|
||||||
|
def __init__(self, toto: str):
|
||||||
|
pass
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
["toto"],
|
||||||
|
False,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
ids=[
|
||||||
|
"direct_transformed",
|
||||||
|
"transformed_with_params",
|
||||||
|
"subclass_transformed",
|
||||||
|
"both_transformed",
|
||||||
|
"intermediate_not_transformed",
|
||||||
|
"init_false",
|
||||||
|
"init_false_multiple",
|
||||||
|
"custom_init",
|
||||||
|
],
|
||||||
)
|
)
|
||||||
def test_dataclass_signature(Script, skip_pre_python37, start, start_params):
|
def test_dataclass_signature(
|
||||||
|
Script, skip_pre_python37, start, start_params, include_params, environment
|
||||||
|
):
|
||||||
|
if environment.version_info < (3, 8):
|
||||||
|
# Final is not yet supported
|
||||||
|
price_type = "float"
|
||||||
|
price_type_infer = "float"
|
||||||
|
else:
|
||||||
|
price_type = "Final[float]"
|
||||||
|
price_type_infer = "object"
|
||||||
|
|
||||||
|
code = dedent(
|
||||||
|
f"""
|
||||||
|
name: str
|
||||||
|
foo = 3
|
||||||
|
blob: ClassVar[str]
|
||||||
|
price: {price_type}
|
||||||
|
quantity: int = 0.0
|
||||||
|
|
||||||
|
X("""
|
||||||
|
)
|
||||||
|
|
||||||
|
code = (
|
||||||
|
"from dataclasses import dataclass\n"
|
||||||
|
+ "from typing import ClassVar, Final\n"
|
||||||
|
+ start
|
||||||
|
+ code
|
||||||
|
)
|
||||||
|
|
||||||
|
sig, = Script(code).get_signatures()
|
||||||
|
expected_params = (
|
||||||
|
[*start_params, "name", "price", "quantity"]
|
||||||
|
if include_params
|
||||||
|
else [*start_params]
|
||||||
|
)
|
||||||
|
assert [p.name for p in sig.params] == expected_params
|
||||||
|
|
||||||
|
if include_params:
|
||||||
|
quantity, = sig.params[-1].infer()
|
||||||
|
assert quantity.name == 'int'
|
||||||
|
price, = sig.params[-2].infer()
|
||||||
|
assert price.name == price_type_infer
|
||||||
|
|
||||||
|
|
||||||
|
dataclass_transform_cases = [
|
||||||
|
# Attributes on the decorated class and its base classes
|
||||||
|
# are not considered to be fields.
|
||||||
|
# 1/ Declare dataclass transformer
|
||||||
|
# Base Class
|
||||||
|
['@dataclass_transform\nclass X:', [], False],
|
||||||
|
# Base Class with params
|
||||||
|
['@dataclass_transform(eq_default=True)\nclass X:', [], False],
|
||||||
|
# Subclass
|
||||||
|
[dedent('''
|
||||||
|
class Y():
|
||||||
|
y: int
|
||||||
|
@dataclass_transform
|
||||||
|
class X(Y):'''), [], False],
|
||||||
|
# 2/ Declare dataclass transformed
|
||||||
|
# Class based
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
class Y():
|
||||||
|
y: int
|
||||||
|
z = 5
|
||||||
|
class X(Y):'''), [], True],
|
||||||
|
# Class based with params
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform(eq_default=True)
|
||||||
|
class Y():
|
||||||
|
y: int
|
||||||
|
z = 5
|
||||||
|
class X(Y):'''), [], True],
|
||||||
|
# Decorator based
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
def create_model():
|
||||||
|
pass
|
||||||
|
@create_model
|
||||||
|
class X:'''), [], True],
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
def create_model():
|
||||||
|
pass
|
||||||
|
class Y:
|
||||||
|
y: int
|
||||||
|
@create_model
|
||||||
|
class X(Y):'''), [], True],
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
def create_model():
|
||||||
|
pass
|
||||||
|
@create_model
|
||||||
|
class Y:
|
||||||
|
y: int
|
||||||
|
@create_model
|
||||||
|
class X(Y):'''), ["y"], True],
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
def create_model():
|
||||||
|
pass
|
||||||
|
@create_model
|
||||||
|
class Y:
|
||||||
|
y: int
|
||||||
|
class Z(Y):
|
||||||
|
z: int
|
||||||
|
@create_model
|
||||||
|
class X(Z):'''), ["y"], True],
|
||||||
|
# Metaclass based
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
class ModelMeta():
|
||||||
|
y: int
|
||||||
|
z = 5
|
||||||
|
class ModelBase(metaclass=ModelMeta):
|
||||||
|
t: int
|
||||||
|
p = 5
|
||||||
|
class X(ModelBase):'''), [], True],
|
||||||
|
# 3/ Init custom init
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform()
|
||||||
|
class Y():
|
||||||
|
y: int
|
||||||
|
z = 5
|
||||||
|
class X(Y):
|
||||||
|
def __init__(self, toto: str):
|
||||||
|
pass
|
||||||
|
'''), ["toto"], False],
|
||||||
|
# 4/ init=false
|
||||||
|
# Class based
|
||||||
|
# WARNING: Unsupported
|
||||||
|
# [dedent('''
|
||||||
|
# @dataclass_transform
|
||||||
|
# class Y():
|
||||||
|
# y: int
|
||||||
|
# z = 5
|
||||||
|
# def __init_subclass__(
|
||||||
|
# cls,
|
||||||
|
# *,
|
||||||
|
# init: bool = False,
|
||||||
|
# )
|
||||||
|
# class X(Y):'''), [], False],
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
class Y():
|
||||||
|
y: int
|
||||||
|
z = 5
|
||||||
|
def __init_subclass__(
|
||||||
|
cls,
|
||||||
|
*,
|
||||||
|
init: bool = False,
|
||||||
|
)
|
||||||
|
class X(Y, init=True):'''), [], True],
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
class Y():
|
||||||
|
y: int
|
||||||
|
z = 5
|
||||||
|
def __init_subclass__(
|
||||||
|
cls,
|
||||||
|
*,
|
||||||
|
init: bool = False,
|
||||||
|
)
|
||||||
|
class X(Y, init=False):'''), [], False],
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
class Y():
|
||||||
|
y: int
|
||||||
|
z = 5
|
||||||
|
class X(Y, init=False):'''), [], False],
|
||||||
|
# Decorator based
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
def create_model(init=False):
|
||||||
|
pass
|
||||||
|
@create_model()
|
||||||
|
class X:'''), [], False],
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
def create_model(init=False):
|
||||||
|
pass
|
||||||
|
@create_model(init=True)
|
||||||
|
class X:'''), [], True],
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
def create_model(init=False):
|
||||||
|
pass
|
||||||
|
@create_model(init=False)
|
||||||
|
class X:'''), [], False],
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
def create_model():
|
||||||
|
pass
|
||||||
|
@create_model(init=False)
|
||||||
|
class X:'''), [], False],
|
||||||
|
# Metaclass based
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
class ModelMeta():
|
||||||
|
y: int
|
||||||
|
z = 5
|
||||||
|
def __new__(
|
||||||
|
cls,
|
||||||
|
name,
|
||||||
|
bases,
|
||||||
|
namespace,
|
||||||
|
*,
|
||||||
|
init: bool = False,
|
||||||
|
):
|
||||||
|
...
|
||||||
|
class ModelBase(metaclass=ModelMeta):
|
||||||
|
t: int
|
||||||
|
p = 5
|
||||||
|
class X(ModelBase):'''), [], False],
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
class ModelMeta():
|
||||||
|
y: int
|
||||||
|
z = 5
|
||||||
|
def __new__(
|
||||||
|
cls,
|
||||||
|
name,
|
||||||
|
bases,
|
||||||
|
namespace,
|
||||||
|
*,
|
||||||
|
init: bool = False,
|
||||||
|
):
|
||||||
|
...
|
||||||
|
class ModelBase(metaclass=ModelMeta):
|
||||||
|
t: int
|
||||||
|
p = 5
|
||||||
|
class X(ModelBase, init=True):'''), [], True],
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
class ModelMeta():
|
||||||
|
y: int
|
||||||
|
z = 5
|
||||||
|
def __new__(
|
||||||
|
cls,
|
||||||
|
name,
|
||||||
|
bases,
|
||||||
|
namespace,
|
||||||
|
*,
|
||||||
|
init: bool = False,
|
||||||
|
):
|
||||||
|
...
|
||||||
|
class ModelBase(metaclass=ModelMeta):
|
||||||
|
t: int
|
||||||
|
p = 5
|
||||||
|
class X(ModelBase, init=False):'''), [], False],
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
class ModelMeta():
|
||||||
|
y: int
|
||||||
|
z = 5
|
||||||
|
class ModelBase(metaclass=ModelMeta):
|
||||||
|
t: int
|
||||||
|
p = 5
|
||||||
|
class X(ModelBase, init=False):'''), [], False],
|
||||||
|
# 4/ Other parameters
|
||||||
|
# Class based
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
class Y():
|
||||||
|
y: int
|
||||||
|
z = 5
|
||||||
|
class X(Y, eq=True):'''), [], True],
|
||||||
|
# Decorator based
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
def create_model():
|
||||||
|
pass
|
||||||
|
@create_model(eq=True)
|
||||||
|
class X:'''), [], True],
|
||||||
|
# Metaclass based
|
||||||
|
[dedent('''
|
||||||
|
@dataclass_transform
|
||||||
|
class ModelMeta():
|
||||||
|
y: int
|
||||||
|
z = 5
|
||||||
|
class ModelBase(metaclass=ModelMeta):
|
||||||
|
t: int
|
||||||
|
p = 5
|
||||||
|
class X(ModelBase, eq=True):'''), [], True],
|
||||||
|
]
|
||||||
|
|
||||||
|
ids = [
|
||||||
|
"direct_transformer",
|
||||||
|
"transformer_with_params",
|
||||||
|
"subclass_transformer",
|
||||||
|
"base_transformed",
|
||||||
|
"base_transformed_with_params",
|
||||||
|
"decorator_transformed_direct",
|
||||||
|
"decorator_transformed_subclass",
|
||||||
|
"decorator_transformed_both",
|
||||||
|
"decorator_transformed_intermediate_not",
|
||||||
|
"metaclass_transformed",
|
||||||
|
"custom_init",
|
||||||
|
# "base_transformed_init_false_dataclass_init_default",
|
||||||
|
"base_transformed_init_false_dataclass_init_true",
|
||||||
|
"base_transformed_init_false_dataclass_init_false",
|
||||||
|
"base_transformed_init_default_dataclass_init_false",
|
||||||
|
"decorator_transformed_init_false_dataclass_init_default",
|
||||||
|
"decorator_transformed_init_false_dataclass_init_true",
|
||||||
|
"decorator_transformed_init_false_dataclass_init_false",
|
||||||
|
"decorator_transformed_init_default_dataclass_init_false",
|
||||||
|
"metaclass_transformed_init_false_dataclass_init_default",
|
||||||
|
"metaclass_transformed_init_false_dataclass_init_true",
|
||||||
|
"metaclass_transformed_init_false_dataclass_init_false",
|
||||||
|
"metaclass_transformed_init_default_dataclass_init_false",
|
||||||
|
"base_transformed_other_parameters",
|
||||||
|
"decorator_transformed_other_parameters",
|
||||||
|
"metaclass_transformed_other_parameters",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'start, start_params, include_params', dataclass_transform_cases, ids=ids
|
||||||
|
)
|
||||||
|
def test_extensions_dataclass_transform_signature(
|
||||||
|
Script, skip_pre_python37, start, start_params, include_params, environment
|
||||||
|
):
|
||||||
|
has_typing_ext = bool(Script('import typing_extensions').infer())
|
||||||
|
if not has_typing_ext:
|
||||||
|
raise pytest.skip("typing_extensions needed in target environment to run this test")
|
||||||
|
|
||||||
|
if environment.version_info < (3, 8):
|
||||||
|
# Final is not yet supported
|
||||||
|
price_type = "float"
|
||||||
|
price_type_infer = "float"
|
||||||
|
else:
|
||||||
|
price_type = "Final[float]"
|
||||||
|
price_type_infer = "object"
|
||||||
|
|
||||||
|
code = dedent(
|
||||||
|
f"""
|
||||||
|
name: str
|
||||||
|
foo = 3
|
||||||
|
blob: ClassVar[str]
|
||||||
|
price: {price_type}
|
||||||
|
quantity: int = 0.0
|
||||||
|
|
||||||
|
X("""
|
||||||
|
)
|
||||||
|
|
||||||
|
code = (
|
||||||
|
"from typing_extensions import dataclass_transform\n"
|
||||||
|
+ "from typing import ClassVar, Final\n"
|
||||||
|
+ start
|
||||||
|
+ code
|
||||||
|
)
|
||||||
|
|
||||||
|
(sig,) = Script(code).get_signatures()
|
||||||
|
expected_params = (
|
||||||
|
[*start_params, "name", "price", "quantity"]
|
||||||
|
if include_params
|
||||||
|
else [*start_params]
|
||||||
|
)
|
||||||
|
assert [p.name for p in sig.params] == expected_params
|
||||||
|
|
||||||
|
if include_params:
|
||||||
|
quantity, = sig.params[-1].infer()
|
||||||
|
assert quantity.name == 'int'
|
||||||
|
price, = sig.params[-2].infer()
|
||||||
|
assert price.name == price_type_infer
|
||||||
|
|
||||||
|
|
||||||
|
def test_dataclass_transform_complete(Script):
|
||||||
|
script = Script('''\
|
||||||
|
@dataclass_transform
|
||||||
|
class Y():
|
||||||
|
y: int
|
||||||
|
z = 5
|
||||||
|
|
||||||
|
class X(Y):
|
||||||
|
name: str
|
||||||
|
foo = 3
|
||||||
|
|
||||||
|
def f(x: X):
|
||||||
|
x.na''')
|
||||||
|
completion, = script.complete()
|
||||||
|
assert completion.description == 'name: str'
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"start, start_params, include_params", dataclass_transform_cases, ids=ids
|
||||||
|
)
|
||||||
|
def test_dataclass_transform_signature(
|
||||||
|
Script, skip_pre_python311, start, start_params, include_params
|
||||||
|
):
|
||||||
|
code = dedent('''
|
||||||
|
name: str
|
||||||
|
foo = 3
|
||||||
|
blob: ClassVar[str]
|
||||||
|
price: Final[float]
|
||||||
|
quantity: int = 0.0
|
||||||
|
|
||||||
|
X(''')
|
||||||
|
|
||||||
|
code = (
|
||||||
|
"from typing import dataclass_transform\n"
|
||||||
|
+ "from typing import ClassVar, Final\n"
|
||||||
|
+ start
|
||||||
|
+ code
|
||||||
|
)
|
||||||
|
|
||||||
|
sig, = Script(code).get_signatures()
|
||||||
|
expected_params = (
|
||||||
|
[*start_params, "name", "price", "quantity"]
|
||||||
|
if include_params
|
||||||
|
else [*start_params]
|
||||||
|
)
|
||||||
|
assert [p.name for p in sig.params] == expected_params
|
||||||
|
|
||||||
|
if include_params:
|
||||||
|
quantity, = sig.params[-1].infer()
|
||||||
|
assert quantity.name == 'int'
|
||||||
|
price, = sig.params[-2].infer()
|
||||||
|
assert price.name == 'object'
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'start, start_params', [
|
||||||
|
['@define\nclass X:', []],
|
||||||
|
['@frozen\nclass X:', []],
|
||||||
|
['@define(eq=True)\nclass X:', []],
|
||||||
|
[dedent('''
|
||||||
|
class Y():
|
||||||
|
y: int
|
||||||
|
@define
|
||||||
|
class X(Y):'''), []],
|
||||||
|
[dedent('''
|
||||||
|
@define
|
||||||
|
class Y():
|
||||||
|
y: int
|
||||||
|
z = 5
|
||||||
|
@define
|
||||||
|
class X(Y):'''), ['y']],
|
||||||
|
],
|
||||||
|
ids=["define", "frozen", "define_customized", "define_subclass", "define_both"]
|
||||||
|
)
|
||||||
|
def test_attrs_signature(Script, skip_pre_python37, start, start_params):
|
||||||
|
has_attrs = bool(Script('import attrs').infer())
|
||||||
|
if not has_attrs:
|
||||||
|
raise pytest.skip("attrs needed in target environment to run this test")
|
||||||
|
|
||||||
code = dedent('''
|
code = dedent('''
|
||||||
name: str
|
name: str
|
||||||
foo = 3
|
foo = 3
|
||||||
@@ -346,7 +858,8 @@ def test_dataclass_signature(Script, skip_pre_python37, start, start_params):
|
|||||||
|
|
||||||
X(''')
|
X(''')
|
||||||
|
|
||||||
code = 'from dataclasses import dataclass\n' + start + code
|
# attrs exposes two namespaces
|
||||||
|
code = 'from attrs import define, frozen\n' + start + code
|
||||||
|
|
||||||
sig, = Script(code).get_signatures()
|
sig, = Script(code).get_signatures()
|
||||||
assert [p.name for p in sig.params] == start_params + ['name', 'price', 'quantity']
|
assert [p.name for p in sig.params] == start_params + ['name', 'price', 'quantity']
|
||||||
|
|||||||
@@ -30,14 +30,16 @@ def test_paths_from_assignment(Script):
|
|||||||
assert paths('sys.path, other = ["a"], 2') == set()
|
assert paths('sys.path, other = ["a"], 2') == set()
|
||||||
|
|
||||||
|
|
||||||
def test_venv_and_pths(venv_path):
|
def test_venv_and_pths(venv_path, environment):
|
||||||
pjoin = os.path.join
|
pjoin = os.path.join
|
||||||
|
|
||||||
site_pkg_path = pjoin(venv_path, 'lib')
|
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
site_pkg_path = pjoin(site_pkg_path, 'site-packages')
|
if environment.version_info < (3, 11):
|
||||||
|
site_pkg_path = pjoin(venv_path, 'lib', 'site-packages')
|
||||||
|
else:
|
||||||
|
site_pkg_path = pjoin(venv_path, 'Lib', 'site-packages')
|
||||||
else:
|
else:
|
||||||
site_pkg_path = glob(pjoin(site_pkg_path, 'python*', 'site-packages'))[0]
|
site_pkg_path = glob(pjoin(venv_path, 'lib', 'python*', 'site-packages'))[0]
|
||||||
shutil.rmtree(site_pkg_path)
|
shutil.rmtree(site_pkg_path)
|
||||||
shutil.copytree(get_example_dir('sample_venvs', 'pth_directory'), site_pkg_path)
|
shutil.copytree(get_example_dir('sample_venvs', 'pth_directory'), site_pkg_path)
|
||||||
|
|
||||||
@@ -46,8 +48,8 @@ def test_venv_and_pths(venv_path):
|
|||||||
|
|
||||||
ETALON = [
|
ETALON = [
|
||||||
# For now disable egg-links. I have no idea how they work... ~ dave
|
# For now disable egg-links. I have no idea how they work... ~ dave
|
||||||
#pjoin('/path', 'from', 'egg-link'),
|
# pjoin('/path', 'from', 'egg-link'),
|
||||||
#pjoin(site_pkg_path, '.', 'relative', 'egg-link', 'path'),
|
# pjoin(site_pkg_path, '.', 'relative', 'egg-link', 'path'),
|
||||||
site_pkg_path,
|
site_pkg_path,
|
||||||
pjoin(site_pkg_path, 'dir-from-foo-pth'),
|
pjoin(site_pkg_path, 'dir-from-foo-pth'),
|
||||||
'/foo/smth.py:module',
|
'/foo/smth.py:module',
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user