forked from VimPlug/jedi
Compare commits
307 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 | ||
|
|
fae26fa7a4 | ||
|
|
a276710f66 | ||
|
|
aa8eed8da4 | ||
|
|
b2e647d598 | ||
|
|
ec9b453379 | ||
|
|
84d086a47b | ||
|
|
8bc9c8cda2 | ||
|
|
a17b958078 | ||
|
|
656ecf502d | ||
|
|
b846043117 | ||
|
|
6fa91726bf | ||
|
|
42508d9309 | ||
|
|
8847848a03 | ||
|
|
8bd969c24a | ||
|
|
458bb30884 | ||
|
|
515e07227b | ||
|
|
6cb5804227 | ||
|
|
e580d1f4d9 | ||
|
|
195695edd3 | ||
|
|
42c5276e04 | ||
|
|
bb5bed4937 | ||
|
|
d872eef1a7 | ||
|
|
53e837055f | ||
|
|
65bc1c117b | ||
|
|
eab1b8be8b | ||
|
|
3cf98f6ba1 | ||
|
|
8808b5b64b | ||
|
|
fe50352f9c | ||
|
|
96b4330ef9 | ||
|
|
1d944943c3 | ||
|
|
78a95f4751 | ||
|
|
599a1c3ee1 | ||
|
|
6814a7336c | ||
|
|
070f191f55 | ||
|
|
11e67ed319 | ||
|
|
ab2eb570a8 | ||
|
|
aa265a44e1 | ||
|
|
25a3e31ca8 | ||
|
|
87388ae00f | ||
|
|
2d11e02fdb | ||
|
|
392dcdf015 | ||
|
|
b9fd84e11c | ||
|
|
75624f0e3c | ||
|
|
6ad62e18d2 | ||
|
|
6787719c28 | ||
|
|
bb40390225 | ||
|
|
0d15347210 | ||
|
|
41652507b3 | ||
|
|
41fb6a0cde | ||
|
|
a340fe077e | ||
|
|
dcea842ac2 | ||
|
|
ce5619cabb | ||
|
|
0eb6720c11 | ||
|
|
ee30843f22 | ||
|
|
613cb08325 | ||
|
|
9f41153eb2 | ||
|
|
387d73990b | ||
|
|
47d0318fa6 | ||
|
|
7555dc0d45 | ||
|
|
2a8b212af7 | ||
|
|
837cb1106a | ||
|
|
b6fd81f1e1 | ||
|
|
0ff532b937 | ||
|
|
b9067ccdbb | ||
|
|
44d77523b3 | ||
|
|
6279791b24 | ||
|
|
4597c7ebe7 | ||
|
|
e6f18df1d2 | ||
|
|
3428a24af0 | ||
|
|
7a3d1f7cee | ||
|
|
8ef2ce232c | ||
|
|
4ab7a53c19 | ||
|
|
c5fb2985a3 | ||
|
|
ca2c732d66 | ||
|
|
2ec3d72151 | ||
|
|
02d43caa5e | ||
|
|
55c7e4eb49 | ||
|
|
7d160f96f6 | ||
|
|
1ccc63e83d | ||
|
|
971913be35 | ||
|
|
36ea6b3285 | ||
|
|
85f45771f1 | ||
|
|
30e702de11 | ||
|
|
778442a972 | ||
|
|
4f34712858 | ||
|
|
d821451a64 | ||
|
|
92d96ac336 | ||
|
|
c64e33173a | ||
|
|
5d2aed34f4 | ||
|
|
04c1c0f871 | ||
|
|
0f128c6deb | ||
|
|
8373ef079f | ||
|
|
227cbde169 | ||
|
|
1f06e6f0c9 | ||
|
|
2d3b8ac8df | ||
|
|
fa6072b4fa | ||
|
|
aae2f7c49a | ||
|
|
52443daf12 | ||
|
|
86d57edda4 | ||
|
|
7298350e76 | ||
|
|
3184264b3b | ||
|
|
d4a1657b2e | ||
|
|
bea401912f | ||
|
|
3e4070bbb3 | ||
|
|
3d7ad50f57 | ||
|
|
85ec94cf65 | ||
|
|
0cc5c974f6 | ||
|
|
6f76bb945a | ||
|
|
239a3730a6 | ||
|
|
8740ff2691 |
10
.gitattributes
vendored
Normal file
10
.gitattributes
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# all end-of-lines are normalized to LF when written to the repository
|
||||||
|
# https://git-scm.com/docs/gitattributes#_text
|
||||||
|
* text=auto
|
||||||
|
|
||||||
|
# force all text files on the working dir to have LF line endings
|
||||||
|
# https://git-scm.com/docs/gitattributes#_eol
|
||||||
|
* text eol=lf
|
||||||
|
|
||||||
|
# PNGs are not text and should not be normalized
|
||||||
|
*.png -text
|
||||||
75
.github/workflows/ci.yml
vendored
Normal file
75
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
name: ci
|
||||||
|
on: [push, pull_request, workflow_dispatch]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
tests:
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu-20.04, windows-2019]
|
||||||
|
python-version: ["3.13", "3.12", "3.11", "3.10", "3.9", "3.8", "3.7", "3.6"]
|
||||||
|
environment: ['3.8', '3.13', '3.12', '3.11', '3.10', '3.9', '3.7', '3.6', 'interpreter']
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
|
||||||
|
- uses: actions/setup-python@v5
|
||||||
|
if: ${{ matrix.environment != 'interpreter' }}
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.environment }}
|
||||||
|
allow-prereleases: true
|
||||||
|
|
||||||
|
- uses: actions/setup-python@v5
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.python-version }}
|
||||||
|
allow-prereleases: true
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: 'pip install .[testing]'
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: python -m pytest
|
||||||
|
env:
|
||||||
|
JEDI_TEST_ENVIRONMENT: ${{ matrix.environment }}
|
||||||
|
|
||||||
|
code-quality:
|
||||||
|
runs-on: ubuntu-20.04
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: 'pip install .[qa]'
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: |
|
||||||
|
python -m flake8 jedi test setup.py
|
||||||
|
python -m mypy jedi sith.py setup.py
|
||||||
|
|
||||||
|
coverage:
|
||||||
|
runs-on: ubuntu-20.04
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: 'pip install .[testing] coverage'
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: |
|
||||||
|
python -m coverage run --source jedi -m pytest
|
||||||
|
python -m coverage report
|
||||||
|
|
||||||
|
- name: Upload coverage data
|
||||||
|
run: |
|
||||||
|
pip install --quiet codecov coveralls
|
||||||
|
python -m coverage xml
|
||||||
|
python -m coverage report -m
|
||||||
|
bash <(curl -s https://codecov.io/bash) -X gcov -X coveragepy -X search -X fix -X xcode -f coverage.xml
|
||||||
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,16 @@
|
|||||||
|
version: 2
|
||||||
|
|
||||||
python:
|
python:
|
||||||
pip_install: true
|
install:
|
||||||
|
- method: pip
|
||||||
|
path: .
|
||||||
|
extra_requirements:
|
||||||
|
- docs
|
||||||
|
|
||||||
|
submodules:
|
||||||
|
include: all
|
||||||
|
|
||||||
|
build:
|
||||||
|
os: ubuntu-22.04
|
||||||
|
tools:
|
||||||
|
python: "3.11"
|
||||||
|
|||||||
74
.travis.yml
74
.travis.yml
@@ -1,74 +0,0 @@
|
|||||||
dist: xenial
|
|
||||||
language: python
|
|
||||||
python:
|
|
||||||
- 3.9-dev
|
|
||||||
- 3.8
|
|
||||||
- 3.7
|
|
||||||
- 3.6
|
|
||||||
|
|
||||||
env:
|
|
||||||
- JEDI_TEST_ENVIRONMENT=38
|
|
||||||
- JEDI_TEST_ENVIRONMENT=39
|
|
||||||
- JEDI_TEST_ENVIRONMENT=37
|
|
||||||
- JEDI_TEST_ENVIRONMENT=36
|
|
||||||
- JEDI_TEST_ENVIRONMENT=interpreter
|
|
||||||
|
|
||||||
matrix:
|
|
||||||
include:
|
|
||||||
- python: 3.8
|
|
||||||
script:
|
|
||||||
- 'pip install coverage'
|
|
||||||
- 'coverage run --source jedi -m pytest'
|
|
||||||
- 'coverage report'
|
|
||||||
after_script:
|
|
||||||
- |
|
|
||||||
pip install --quiet codecov coveralls
|
|
||||||
coverage xml
|
|
||||||
coverage report -m
|
|
||||||
coveralls
|
|
||||||
bash <(curl -s https://codecov.io/bash) -X gcov -X coveragepy -X search -X fix -X xcode -f coverage.xml
|
|
||||||
- python: 3.8
|
|
||||||
install:
|
|
||||||
- 'pip install .[qa]'
|
|
||||||
script:
|
|
||||||
- 'flake8 jedi setup.py'
|
|
||||||
- 'mypy jedi sith.py'
|
|
||||||
install:
|
|
||||||
- sudo apt-get -y install python3-venv
|
|
||||||
- pip install .[testing]
|
|
||||||
script:
|
|
||||||
- |
|
|
||||||
# Setup/install Python for $JEDI_TEST_ENVIRONMENT.
|
|
||||||
set -ex
|
|
||||||
test_env_version=${JEDI_TEST_ENVIRONMENT:0:1}.${JEDI_TEST_ENVIRONMENT:1:1}
|
|
||||||
if [ "$TRAVIS_PYTHON_VERSION" != "$test_env_version" ] && [ "$JEDI_TEST_ENVIRONMENT" != "interpreter" ]; then
|
|
||||||
python_bin=python$test_env_version
|
|
||||||
python_path="$(which $python_bin || true)"
|
|
||||||
if [ -z "$python_path" ]; then
|
|
||||||
# Only required for JEDI_TEST_ENVIRONMENT=38, because it's not always
|
|
||||||
# available.
|
|
||||||
download_name=python-$test_env_version
|
|
||||||
if [ "$JEDI_TEST_ENVIRONMENT" == "39" ]; then
|
|
||||||
wget https://storage.googleapis.com/travis-ci-language-archives/python/binaries/ubuntu/16.04/x86_64/python-3.9-dev.tar.bz2
|
|
||||||
sudo tar xjf python-3.9-dev.tar.bz2 --directory / opt/python
|
|
||||||
ln -s "/opt/python/3.9-dev/bin/python" /home/travis/bin/python3.9
|
|
||||||
else
|
|
||||||
wget https://s3.amazonaws.com/travis-python-archives/binaries/ubuntu/16.04/x86_64/$download_name.tar.bz2
|
|
||||||
sudo tar xjf $download_name.tar.bz2 --directory / opt/python
|
|
||||||
ln -s "/opt/python/${test_env_version}/bin/python" /home/travis/bin/$python_bin
|
|
||||||
fi
|
|
||||||
elif [ "${python_path#/opt/pyenv/shims}" != "$python_path" ]; then
|
|
||||||
# Activate pyenv version (required with JEDI_TEST_ENVIRONMENT=36).
|
|
||||||
pyenv_bin="$(pyenv whence --path "$python_bin" | head -n1)"
|
|
||||||
ln -s "$pyenv_bin" /home/travis/bin/$python_bin
|
|
||||||
fi
|
|
||||||
$python_bin --version
|
|
||||||
python_ver=$($python_bin -c 'import sys; print("%d%d" % sys.version_info[0:2])')
|
|
||||||
if [ "$JEDI_TEST_ENVIRONMENT" != "$python_ver" ]; then
|
|
||||||
echo "Unexpected Python version for $JEDI_TEST_ENVIRONMENT: $python_ver"
|
|
||||||
set +ex
|
|
||||||
exit 2
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
set +ex
|
|
||||||
- pytest
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
Main Authors
|
Main Authors
|
||||||
------------
|
------------
|
||||||
|
|
||||||
- David Halter (@davidhalter) <davidhalter88@gmail.com>
|
- David Halter (@davidhalter) <davidhalter88@gmail.com>
|
||||||
@@ -61,6 +61,11 @@ Code Contributors
|
|||||||
- Vladislav Serebrennikov (@endilll)
|
- Vladislav Serebrennikov (@endilll)
|
||||||
- Andrii Kolomoiets (@muffinmad)
|
- Andrii Kolomoiets (@muffinmad)
|
||||||
- Leo Ryu (@Leo-Ryu)
|
- Leo Ryu (@Leo-Ryu)
|
||||||
|
- 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,42 @@ 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)
|
||||||
|
+++++++++++++++++++
|
||||||
|
|
||||||
|
- Implict namespaces are now a separate types in ``Name().type``
|
||||||
|
- Python 3.10 support
|
||||||
|
- Mostly bugfixes
|
||||||
|
|
||||||
0.18.0 (2020-12-25)
|
0.18.0 (2020-12-25)
|
||||||
+++++++++++++++++++
|
+++++++++++++++++++
|
||||||
|
|
||||||
|
|||||||
19
README.rst
19
README.rst
@@ -10,17 +10,9 @@ 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://travis-ci.org/davidhalter/jedi.svg?branch=master
|
.. image:: https://github.com/davidhalter/jedi/workflows/ci/badge.svg?branch=master
|
||||||
:target: https://travis-ci.org/davidhalter/jedi
|
:target: https://github.com/davidhalter/jedi/actions
|
||||||
:alt: Linux Tests
|
:alt: Tests
|
||||||
|
|
||||||
.. image:: https://ci.appveyor.com/api/projects/status/mgva3bbawyma1new/branch/master?svg=true
|
|
||||||
:target: https://ci.appveyor.com/project/davidhalter/jedi/branch/master
|
|
||||||
:alt: Windows Tests
|
|
||||||
|
|
||||||
.. image:: https://coveralls.io/repos/davidhalter/jedi/badge.svg?branch=master
|
|
||||||
:target: https://coveralls.io/r/davidhalter/jedi
|
|
||||||
:alt: Coverage status
|
|
||||||
|
|
||||||
.. image:: https://pepy.tech/badge/jedi
|
.. image:: https://pepy.tech/badge/jedi
|
||||||
:target: https://pepy.tech/project/jedi
|
:target: https://pepy.tech/project/jedi
|
||||||
@@ -50,7 +42,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>`_
|
||||||
|
|
||||||
@@ -59,7 +51,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_:
|
||||||
|
|||||||
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.
|
||||||
17
appveyor.yml
17
appveyor.yml
@@ -1,17 +0,0 @@
|
|||||||
environment:
|
|
||||||
matrix:
|
|
||||||
- PYTHON_PATH: C:\Python37
|
|
||||||
JEDI_TEST_ENVIRONMENT: 37
|
|
||||||
- PYTHON_PATH: C:\Python37
|
|
||||||
JEDI_TEST_ENVIRONMENT: 36
|
|
||||||
|
|
||||||
- PYTHON_PATH: C:\Python36
|
|
||||||
JEDI_TEST_ENVIRONMENT: 37
|
|
||||||
- PYTHON_PATH: C:\Python36
|
|
||||||
JEDI_TEST_ENVIRONMENT: 36
|
|
||||||
install:
|
|
||||||
- git submodule update --init --recursive
|
|
||||||
- set PATH=%PYTHON_PATH%;%PYTHON_PATH%\Scripts;%PATH%
|
|
||||||
- pip install .[testing]
|
|
||||||
build_script:
|
|
||||||
- pytest
|
|
||||||
@@ -100,7 +100,9 @@ def environment(request):
|
|||||||
if request.config.option.interpreter_env or version == 'interpreter':
|
if request.config.option.interpreter_env or version == 'interpreter':
|
||||||
return InterpreterEnvironment()
|
return InterpreterEnvironment()
|
||||||
|
|
||||||
return get_system_environment(version[0] + '.' + version[1:])
|
if '.' not in version:
|
||||||
|
version = version[0] + '.' + version[1:]
|
||||||
|
return get_system_environment(version)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='session')
|
@pytest.fixture(scope='session')
|
||||||
@@ -138,6 +140,11 @@ def goto_or_help_or_infer(request, Script):
|
|||||||
return do
|
return do
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='session', params=['goto', 'complete', 'help'])
|
||||||
|
def goto_or_complete(request, Script):
|
||||||
|
return lambda code, *args, **kwargs: getattr(Script(code), request.param)(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='session')
|
@pytest.fixture(scope='session')
|
||||||
def has_django(environment):
|
def has_django(environment):
|
||||||
script = jedi.Script('import django', environment=environment)
|
script = jedi.Script('import django', environment=environment)
|
||||||
|
|||||||
@@ -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:
|
||||||
|
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ Supported Python Features
|
|||||||
Limitations
|
Limitations
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
In general Jedi's limit are quite high, but for very big projects or very
|
In general Jedi's limit is quite high, but for very big projects or very
|
||||||
complex code, sometimes Jedi intentionally stops type inference, to avoid
|
complex code, sometimes Jedi intentionally stops type inference, to avoid
|
||||||
hanging for a long time.
|
hanging for a long time.
|
||||||
|
|
||||||
@@ -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.
|
||||||
|
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ easy as::
|
|||||||
|
|
||||||
python3.8 -m pytest
|
python3.8 -m pytest
|
||||||
|
|
||||||
Tests are also run automatically on `Travis CI
|
Tests are also run automatically on `GitHub Actions
|
||||||
<https://travis-ci.org/davidhalter/jedi/>`_.
|
<https://github.com/davidhalter/jedi/actions>`_.
|
||||||
|
|
||||||
You want to add a test for |jedi|? Great! We love that. Normally you should
|
You want to add a test for |jedi|? Great! We love that. Normally you should
|
||||||
write your tests as :ref:`Blackbox Tests <blackbox>`. Most tests would
|
write your tests as :ref:`Blackbox Tests <blackbox>`. Most tests would
|
||||||
|
|||||||
@@ -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
|
||||||
~~~~~~~~~~~~
|
~~~~~~~~~~~~
|
||||||
|
|||||||
@@ -18,13 +18,9 @@ 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://travis-ci.org/davidhalter/jedi.svg?branch=master
|
.. image:: https://github.com/davidhalter/jedi/workflows/ci/badge.svg?branch=master
|
||||||
:target: https://travis-ci.org/davidhalter/jedi
|
:target: https://github.com/davidhalter/jedi/actions
|
||||||
:alt: Linux Tests
|
:alt: Tests
|
||||||
|
|
||||||
.. image:: https://ci.appveyor.com/api/projects/status/mgva3bbawyma1new/branch/master?svg=true
|
|
||||||
:target: https://ci.appveyor.com/project/davidhalter/jedi/branch/master
|
|
||||||
:alt: Windows Tests
|
|
||||||
|
|
||||||
.. image:: https://coveralls.io/repos/davidhalter/jedi/badge.svg?branch=master
|
.. image:: https://coveralls.io/repos/davidhalter/jedi/badge.svg?branch=master
|
||||||
:target: https://coveralls.io/r/davidhalter/jedi
|
:target: https://coveralls.io/r/davidhalter/jedi
|
||||||
@@ -73,5 +69,4 @@ mailing list: https://groups.google.com/g/jedi-announce. To subscribe you can
|
|||||||
simply send an empty email to ``jedi-announce+subscribe@googlegroups.com``.
|
simply send an empty email to ``jedi-announce+subscribe@googlegroups.com``.
|
||||||
|
|
||||||
- `Source Code on Github <https://github.com/davidhalter/jedi>`_
|
- `Source Code on Github <https://github.com/davidhalter/jedi>`_
|
||||||
- `Travis Testing <https://travis-ci.org/davidhalter/jedi>`_
|
|
||||||
- `Python Package Index <https://pypi.python.org/pypi/jedi/>`_
|
- `Python Package Index <https://pypi.python.org/pypi/jedi/>`_
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ ad
|
|||||||
load
|
load
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__version__ = '0.18.0'
|
__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,27 +5,24 @@ different Python versions.
|
|||||||
import errno
|
import errno
|
||||||
import sys
|
import sys
|
||||||
import pickle
|
import pickle
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
def cast_path(string):
|
class Unpickler(pickle.Unpickler):
|
||||||
"""
|
def find_class(self, module: str, name: str) -> Any:
|
||||||
Take a bytes or str path and cast it to unicode.
|
# Python 3.13 moved pathlib implementation out of __init__.py as part of
|
||||||
|
# generalising its implementation. Ensure that we support loading
|
||||||
Apparently it is perfectly fine to pass both byte and unicode objects into
|
# pickles from 3.13 on older version of Python. Since 3.13 maintained a
|
||||||
the sys.path. This probably means that byte paths are normal at other
|
# compatible API, pickles from older Python work natively on the newer
|
||||||
places as well.
|
# version.
|
||||||
|
if module == 'pathlib._local':
|
||||||
Since this just really complicates everything and Python 2.7 will be EOL
|
module = 'pathlib'
|
||||||
soon anyway, just go with always strings.
|
return super().find_class(module, name)
|
||||||
"""
|
|
||||||
if isinstance(string, bytes):
|
|
||||||
return str(string, encoding='UTF-8', errors='replace')
|
|
||||||
return str(string)
|
|
||||||
|
|
||||||
|
|
||||||
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:
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ from pathlib import Path
|
|||||||
import parso
|
import parso
|
||||||
from parso.python import tree
|
from parso.python import tree
|
||||||
|
|
||||||
from jedi._compatibility import cast_path
|
|
||||||
from jedi.parser_utils import get_executable_nodes
|
from jedi.parser_utils import get_executable_nodes
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi import settings
|
from jedi import settings
|
||||||
@@ -100,13 +99,15 @@ class Script:
|
|||||||
"""
|
"""
|
||||||
def __init__(self, code=None, *, path=None, environment=None, project=None):
|
def __init__(self, code=None, *, path=None, environment=None, project=None):
|
||||||
self._orig_path = path
|
self._orig_path = path
|
||||||
# An empty path (also empty string) should always result in no path.
|
|
||||||
if isinstance(path, str):
|
if isinstance(path, str):
|
||||||
path = Path(path)
|
path = Path(path)
|
||||||
|
|
||||||
self.path = path.absolute() if path else None
|
self.path = path.absolute() if path else None
|
||||||
|
|
||||||
if code is None:
|
if code is None:
|
||||||
|
if path is None:
|
||||||
|
raise ValueError("Must provide at least one of code or path")
|
||||||
|
|
||||||
# TODO add a better warning than the traceback!
|
# TODO add a better warning than the traceback!
|
||||||
with open(path, 'rb') as f:
|
with open(path, 'rb') as f:
|
||||||
code = f.read()
|
code = f.read()
|
||||||
@@ -152,7 +153,7 @@ class Script:
|
|||||||
if self.path is None:
|
if self.path is None:
|
||||||
file_io = None
|
file_io = None
|
||||||
else:
|
else:
|
||||||
file_io = KnownContentFileIO(cast_path(self.path), self._code)
|
file_io = KnownContentFileIO(self.path, self._code)
|
||||||
if self.path is not None and self.path.suffix == '.pyi':
|
if self.path is not None and self.path.suffix == '.pyi':
|
||||||
# We are in a stub file. Try to load the stub properly.
|
# We are in a stub file. Try to load the stub properly.
|
||||||
stub_module = load_proper_stub_module(
|
stub_module = load_proper_stub_module(
|
||||||
@@ -205,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,
|
||||||
@@ -229,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:
|
||||||
@@ -272,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.
|
||||||
@@ -364,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 ('(', ')', '[', ']'):
|
||||||
@@ -393,12 +404,13 @@ class Script:
|
|||||||
quite hard to do for Jedi, if it is too complicated, Jedi will stop
|
quite hard to do for Jedi, if it is too complicated, Jedi will stop
|
||||||
searching.
|
searching.
|
||||||
|
|
||||||
:param include_builtins: Default ``True``. If ``False``, checks if a reference
|
:param include_builtins: Default ``True``. If ``False``, checks if a definition
|
||||||
is a builtin (e.g. ``sys``) and in that case does not return it.
|
is a builtin (e.g. ``sys``) and in that case does not return it.
|
||||||
:param scope: Default ``'project'``. If ``'file'``, include references in
|
:param scope: Default ``'project'``. If ``'file'``, include references in
|
||||||
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'):
|
||||||
@@ -433,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:
|
||||||
@@ -552,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 = [
|
||||||
@@ -580,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``::
|
||||||
@@ -707,9 +721,8 @@ 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, **kwds):
|
def __init__(self, code, namespaces, *, project=None, **kwds):
|
||||||
try:
|
try:
|
||||||
namespaces = [dict(n) for n in namespaces]
|
namespaces = [dict(n) for n in namespaces]
|
||||||
except Exception:
|
except Exception:
|
||||||
@@ -722,16 +735,32 @@ class Interpreter(Script):
|
|||||||
if not isinstance(environment, InterpreterEnvironment):
|
if not isinstance(environment, InterpreterEnvironment):
|
||||||
raise TypeError("The environment needs to be an InterpreterEnvironment subclass.")
|
raise TypeError("The environment needs to be an InterpreterEnvironment subclass.")
|
||||||
|
|
||||||
super().__init__(code, environment=environment,
|
if project is None:
|
||||||
project=Project(Path.cwd()), **kwds)
|
project = Project(Path.cwd())
|
||||||
|
|
||||||
|
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):
|
||||||
|
if self.path is None:
|
||||||
|
file_io = None
|
||||||
|
else:
|
||||||
|
file_io = KnownContentFileIO(self.path, self._code)
|
||||||
tree_module_value = ModuleValue(
|
tree_module_value = ModuleValue(
|
||||||
self._inference_state, self._module_node,
|
self._inference_state, self._module_node,
|
||||||
file_io=KnownContentFileIO(str(self.path), self._code),
|
file_io=file_io,
|
||||||
string_names=('__main__',),
|
string_names=('__main__',),
|
||||||
code_lines=self._code_lines,
|
code_lines=self._code_lines,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ from jedi.inference.compiled.mixed import MixedName
|
|||||||
from jedi.inference.names import ImportName, SubModuleName
|
from jedi.inference.names import ImportName, SubModuleName
|
||||||
from jedi.inference.gradual.stub_value import StubModuleValue
|
from jedi.inference.gradual.stub_value import StubModuleValue
|
||||||
from jedi.inference.gradual.conversion import convert_names, convert_values
|
from jedi.inference.gradual.conversion import convert_names, convert_values
|
||||||
from jedi.inference.base_value import ValueSet
|
from jedi.inference.base_value import ValueSet, HasNoContext
|
||||||
from jedi.api.keywords import KeywordName
|
from jedi.api.keywords import KeywordName
|
||||||
from jedi.api import completion_cache
|
from jedi.api import completion_cache
|
||||||
from jedi.api.helpers import filter_follow_imports
|
from jedi.api.helpers import filter_follow_imports
|
||||||
@@ -37,13 +37,17 @@ def _sort_names_by_start_pos(names):
|
|||||||
return sorted(names, key=lambda s: s.start_pos or (0, 0))
|
return sorted(names, key=lambda s: s.start_pos or (0, 0))
|
||||||
|
|
||||||
|
|
||||||
def defined_names(inference_state, context):
|
def defined_names(inference_state, value):
|
||||||
"""
|
"""
|
||||||
List sub-definitions (e.g., methods in class).
|
List sub-definitions (e.g., methods in class).
|
||||||
|
|
||||||
:type scope: Scope
|
:type scope: Scope
|
||||||
:rtype: list of Name
|
:rtype: list of Name
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
|
context = value.as_context()
|
||||||
|
except HasNoContext:
|
||||||
|
return []
|
||||||
filter = next(context.get_filters())
|
filter = next(context.get_filters())
|
||||||
names = [name for name in filter.values()]
|
names = [name for name in filter.values()]
|
||||||
return [Name(inference_state, n) for n in _sort_names_by_start_pos(names)]
|
return [Name(inference_state, n) for n in _sort_names_by_start_pos(names)]
|
||||||
@@ -101,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
|
||||||
|
|
||||||
@@ -550,6 +553,8 @@ class BaseName:
|
|||||||
return ''.join(lines[start_index:index + after + 1])
|
return ''.join(lines[start_index:index + after + 1])
|
||||||
|
|
||||||
def _get_signatures(self, for_docstring=False):
|
def _get_signatures(self, for_docstring=False):
|
||||||
|
if self._name.api_type == 'property':
|
||||||
|
return []
|
||||||
if for_docstring and self._name.api_type == 'statement' and not self.is_stub():
|
if for_docstring and self._name.api_type == 'statement' and not self.is_stub():
|
||||||
# For docstrings we don't resolve signatures if they are simple
|
# For docstrings we don't resolve signatures if they are simple
|
||||||
# statements and not stubs. This is a speed optimization.
|
# statements and not stubs. This is a speed optimization.
|
||||||
@@ -757,7 +762,7 @@ class Name(BaseName):
|
|||||||
"""
|
"""
|
||||||
defs = self._name.infer()
|
defs = self._name.infer()
|
||||||
return sorted(
|
return sorted(
|
||||||
unite(defined_names(self._inference_state, d.as_context()) for d in defs),
|
unite(defined_names(self._inference_state, d) for d in defs),
|
||||||
key=lambda s: s._name.start_pos or (0, 0)
|
key=lambda s: s._name.start_pos or (0, 0)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,8 @@ from jedi.inference import imports
|
|||||||
from jedi.inference.base_value import ValueSet
|
from jedi.inference.base_value import ValueSet
|
||||||
from jedi.inference.helpers import infer_call_of_leaf, parse_dotted_names
|
from jedi.inference.helpers import infer_call_of_leaf, parse_dotted_names
|
||||||
from jedi.inference.context import get_global_filters
|
from jedi.inference.context import get_global_filters
|
||||||
from jedi.inference.value import TreeInstance, ModuleValue
|
from jedi.inference.value import TreeInstance
|
||||||
|
from jedi.inference.docstring_utils import DocstringModule
|
||||||
from jedi.inference.names import ParamNameWrapper, SubModuleName
|
from jedi.inference.names import ParamNameWrapper, SubModuleName
|
||||||
from jedi.inference.gradual.conversion import convert_values, convert_names
|
from jedi.inference.gradual.conversion import convert_values, convert_names
|
||||||
from jedi.parser_utils import cut_value_at_position
|
from jedi.parser_utils import cut_value_at_position
|
||||||
@@ -64,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):
|
||||||
@@ -137,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,
|
||||||
@@ -168,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()))
|
||||||
)
|
)
|
||||||
@@ -194,7 +208,6 @@ class Completion:
|
|||||||
- In args: */**: no completion
|
- In args: */**: no completion
|
||||||
- In params (also lambda): no completion before =
|
- In params (also lambda): no completion before =
|
||||||
"""
|
"""
|
||||||
|
|
||||||
grammar = self._inference_state.grammar
|
grammar = self._inference_state.grammar
|
||||||
self.stack = stack = None
|
self.stack = stack = None
|
||||||
self._position = (
|
self._position = (
|
||||||
@@ -277,6 +290,10 @@ class Completion:
|
|||||||
)
|
)
|
||||||
elif nonterminals[-1] in ('trailer', 'dotted_name') and nodes[-1] == '.':
|
elif nonterminals[-1] in ('trailer', 'dotted_name') and nodes[-1] == '.':
|
||||||
dot = self._module_node.get_leaf_for_position(self._position)
|
dot = self._module_node.get_leaf_for_position(self._position)
|
||||||
|
if dot.type == "endmarker":
|
||||||
|
# This is a bit of a weird edge case, maybe we can somehow
|
||||||
|
# generalize this.
|
||||||
|
dot = leaf.get_previous_leaf()
|
||||||
cached_name, n = self._complete_trailer(dot.get_previous_leaf())
|
cached_name, n = self._complete_trailer(dot.get_previous_leaf())
|
||||||
completion_names += n
|
completion_names += n
|
||||||
elif self._is_parameter_completion():
|
elif self._is_parameter_completion():
|
||||||
@@ -438,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:
|
||||||
@@ -462,12 +480,12 @@ class Completion:
|
|||||||
|
|
||||||
def _complete_code_lines(self, code_lines):
|
def _complete_code_lines(self, code_lines):
|
||||||
module_node = self._inference_state.grammar.parse(''.join(code_lines))
|
module_node = self._inference_state.grammar.parse(''.join(code_lines))
|
||||||
module_value = ModuleValue(
|
module_value = DocstringModule(
|
||||||
self._inference_state,
|
in_module_context=self._module_context,
|
||||||
module_node,
|
inference_state=self._inference_state,
|
||||||
|
module_node=module_node,
|
||||||
code_lines=code_lines,
|
code_lines=code_lines,
|
||||||
)
|
)
|
||||||
module_value.parent_context = self._module_context
|
|
||||||
return Completion(
|
return Completion(
|
||||||
self._inference_state,
|
self._inference_state,
|
||||||
module_value.as_context(),
|
module_value.as_context(),
|
||||||
@@ -627,7 +645,7 @@ def search_in_module(inference_state, module_context, names, wanted_names,
|
|||||||
new_names = []
|
new_names = []
|
||||||
for n in names:
|
for n in names:
|
||||||
if s == n.string_name:
|
if s == n.string_name:
|
||||||
if n.tree_name is not None and n.api_type == 'module' \
|
if n.tree_name is not None and n.api_type in ('module', 'namespace') \
|
||||||
and ignore_imports:
|
and ignore_imports:
|
||||||
continue
|
continue
|
||||||
new_names += complete_trailer(
|
new_names += complete_trailer(
|
||||||
@@ -660,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', 'import_as_name']:
|
||||||
|
for index, child in enumerate(node.children):
|
||||||
|
if child.type == 'name':
|
||||||
|
if (index > 0 and node.children[index - 1].type == "keyword"
|
||||||
|
and node.children[index - 1].value == "as"):
|
||||||
|
continue
|
||||||
|
imported_names.append(child.value)
|
||||||
|
elif child.type == 'import_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.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', '3.7', '3.6']
|
||||||
_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
|
||||||
|
|||||||
@@ -205,7 +205,6 @@ def filter_follow_imports(names, follow_builtin_imports=False):
|
|||||||
|
|
||||||
class CallDetails:
|
class CallDetails:
|
||||||
def __init__(self, bracket_leaf, children, position):
|
def __init__(self, bracket_leaf, children, position):
|
||||||
['bracket_leaf', 'call_index', 'keyword_name_str']
|
|
||||||
self.bracket_leaf = bracket_leaf
|
self.bracket_leaf = bracket_leaf
|
||||||
self._children = children
|
self._children = children
|
||||||
self._position = position
|
self._position = position
|
||||||
@@ -281,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
|
||||||
@@ -307,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.
|
||||||
|
|||||||
@@ -106,7 +106,16 @@ class Project:
|
|||||||
with open(self._get_json_path(self._path), 'w') as f:
|
with open(self._get_json_path(self._path), 'w') as f:
|
||||||
return json.dump((_SERIALIZER_VERSION, data), f)
|
return json.dump((_SERIALIZER_VERSION, data), f)
|
||||||
|
|
||||||
def __init__(self, path, **kwargs):
|
def __init__(
|
||||||
|
self,
|
||||||
|
path,
|
||||||
|
*,
|
||||||
|
environment_path=None,
|
||||||
|
load_unsafe_extensions=False,
|
||||||
|
sys_path=None,
|
||||||
|
added_sys_path=(),
|
||||||
|
smart_sys_path=True,
|
||||||
|
) -> None:
|
||||||
"""
|
"""
|
||||||
:param path: The base path for this project.
|
:param path: The base path for this project.
|
||||||
:param environment_path: The Python executable path, typically the path
|
:param environment_path: The Python executable path, typically the path
|
||||||
@@ -125,25 +134,22 @@ class Project:
|
|||||||
local directories. Otherwise you will have to rely on your packages
|
local directories. Otherwise you will have to rely on your packages
|
||||||
being properly configured on the ``sys.path``.
|
being properly configured on the ``sys.path``.
|
||||||
"""
|
"""
|
||||||
def py2_comp(path, environment_path=None, load_unsafe_extensions=False,
|
|
||||||
sys_path=None, added_sys_path=(), smart_sys_path=True):
|
|
||||||
if isinstance(path, str):
|
|
||||||
path = Path(path).absolute()
|
|
||||||
self._path = path
|
|
||||||
|
|
||||||
self._environment_path = environment_path
|
if isinstance(path, str):
|
||||||
if sys_path is not None:
|
path = Path(path).absolute()
|
||||||
# Remap potential pathlib.Path entries
|
self._path = path
|
||||||
sys_path = list(map(str, sys_path))
|
|
||||||
self._sys_path = sys_path
|
self._environment_path = environment_path
|
||||||
self._smart_sys_path = smart_sys_path
|
if sys_path is not None:
|
||||||
self._load_unsafe_extensions = load_unsafe_extensions
|
|
||||||
self._django = False
|
|
||||||
# Remap potential pathlib.Path entries
|
# Remap potential pathlib.Path entries
|
||||||
self.added_sys_path = list(map(str, added_sys_path))
|
sys_path = list(map(str, sys_path))
|
||||||
"""The sys path that is going to be added at the end of the """
|
self._sys_path = sys_path
|
||||||
|
self._smart_sys_path = smart_sys_path
|
||||||
py2_comp(path, **kwargs)
|
self._load_unsafe_extensions = load_unsafe_extensions
|
||||||
|
self._django = False
|
||||||
|
# Remap potential pathlib.Path entries
|
||||||
|
self.added_sys_path = list(map(str, added_sys_path))
|
||||||
|
"""The sys path that is going to be added at the end of the """
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def path(self):
|
def path(self):
|
||||||
@@ -328,7 +334,8 @@ class Project:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# 2. Search for identifiers in the project.
|
# 2. Search for identifiers in the project.
|
||||||
for module_context in search_in_file_ios(inference_state, file_ios, name):
|
for module_context in search_in_file_ios(inference_state, file_ios,
|
||||||
|
name, complete=complete):
|
||||||
names = get_module_names(module_context.tree_node, all_scopes=all_scopes)
|
names = get_module_names(module_context.tree_node, all_scopes=all_scopes)
|
||||||
names = [module_context.create_name(n) for n in names]
|
names = [module_context.create_name(n) for n in names]
|
||||||
names = _remove_imports(names)
|
names = _remove_imports(names)
|
||||||
@@ -345,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(
|
||||||
@@ -426,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:
|
||||||
@@ -439,5 +444,5 @@ def get_default_project(path=None):
|
|||||||
def _remove_imports(names):
|
def _remove_imports(names):
|
||||||
return [
|
return [
|
||||||
n for n in names
|
n for n in names
|
||||||
if n.tree_name is None or n.api_type != 'module'
|
if n.tree_name is None or n.api_type not in ('module', 'namespace')
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -156,8 +171,8 @@ def rename(inference_state, definitions, new_name):
|
|||||||
def inline(inference_state, names):
|
def inline(inference_state, names):
|
||||||
if not names:
|
if not names:
|
||||||
raise RefactoringError("There is no name under the cursor")
|
raise RefactoringError("There is no name under the cursor")
|
||||||
if any(n.api_type == 'module' for n in names):
|
if any(n.api_type in ('module', 'namespace') for n in names):
|
||||||
raise RefactoringError("Cannot inline imports or modules")
|
raise RefactoringError("Cannot inline imports, modules or namespaces")
|
||||||
if any(n.tree_name is None for n in names):
|
if any(n.tree_name is None for n in names):
|
||||||
raise RefactoringError("Cannot inline builtins/extensions")
|
raise RefactoringError("Cannot inline builtins/extensions")
|
||||||
|
|
||||||
@@ -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,
|
||||||
|
|||||||
@@ -106,10 +106,7 @@ def dbg(message, *args, color='GREEN'):
|
|||||||
debug_function(color, i + 'dbg: ' + message % tuple(repr(a) for a in args))
|
debug_function(color, i + 'dbg: ' + message % tuple(repr(a) for a in args))
|
||||||
|
|
||||||
|
|
||||||
def warning(message, *args, **kwargs):
|
def warning(message, *args, format=True):
|
||||||
format = kwargs.pop('format', True)
|
|
||||||
assert not kwargs
|
|
||||||
|
|
||||||
if debug_function and enable_warning:
|
if debug_function and enable_warning:
|
||||||
i = ' ' * _debug_indent
|
i = ' ' * _debug_indent
|
||||||
if format:
|
if format:
|
||||||
|
|||||||
@@ -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()
|
||||||
@@ -125,7 +126,7 @@ class InferenceState:
|
|||||||
@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 # type: ignore[misc]
|
||||||
@@ -181,8 +182,6 @@ class InferenceState:
|
|||||||
|
|
||||||
def parse_and_get_code(self, code=None, path=None,
|
def parse_and_get_code(self, code=None, path=None,
|
||||||
use_latest_grammar=False, file_io=None, **kwargs):
|
use_latest_grammar=False, file_io=None, **kwargs):
|
||||||
if path is not None:
|
|
||||||
path = str(path)
|
|
||||||
if code is None:
|
if code is None:
|
||||||
if file_io is None:
|
if file_io is None:
|
||||||
file_io = FileIO(path)
|
file_io = FileIO(path)
|
||||||
|
|||||||
@@ -22,6 +22,10 @@ from jedi.cache import memoize_method
|
|||||||
sentinel = object()
|
sentinel = object()
|
||||||
|
|
||||||
|
|
||||||
|
class HasNoContext(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class HelperValueMixin:
|
class HelperValueMixin:
|
||||||
def get_root_context(self):
|
def get_root_context(self):
|
||||||
value = self
|
value = self
|
||||||
@@ -261,7 +265,7 @@ class Value(HelperValueMixin):
|
|||||||
return self.parent_context.is_stub()
|
return self.parent_context.is_stub()
|
||||||
|
|
||||||
def _as_context(self):
|
def _as_context(self):
|
||||||
raise NotImplementedError('Not all values need to be converted to contexts: %s', self)
|
raise HasNoContext
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@@ -293,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`.
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import warnings
|
|||||||
import re
|
import re
|
||||||
import builtins
|
import builtins
|
||||||
import typing
|
import typing
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Optional, Tuple
|
||||||
|
|
||||||
from jedi.inference.compiled.getattr_static import getattr_static
|
from jedi.inference.compiled.getattr_static import getattr_static
|
||||||
|
|
||||||
@@ -38,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()
|
||||||
@@ -145,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())
|
||||||
|
|
||||||
@@ -173,15 +175,15 @@ 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):
|
||||||
return bool(self._obj)
|
return bool(self._obj)
|
||||||
|
|
||||||
def py__file__(self):
|
def py__file__(self) -> Optional[Path]:
|
||||||
try:
|
try:
|
||||||
return self._obj.__file__
|
return Path(self._obj.__file__)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -211,18 +213,39 @@ class DirectObjectAccess:
|
|||||||
def py__getitem__all_values(self):
|
def py__getitem__all_values(self):
|
||||||
if isinstance(self._obj, dict):
|
if isinstance(self._obj, dict):
|
||||||
return [self._create_access_path(v) for v in self._obj.values()]
|
return [self._create_access_path(v) for v in self._obj.values()]
|
||||||
return self.py__iter__list()
|
if isinstance(self._obj, (list, tuple)):
|
||||||
|
return [self._create_access_path(v) for v in self._obj]
|
||||||
|
|
||||||
def py__simple_getitem__(self, index):
|
if self.is_instance():
|
||||||
if type(self._obj) not in ALLOWED_GETITEM_TYPES:
|
cls = DirectObjectAccess(self._inference_state, self._obj.__class__)
|
||||||
|
return cls.py__getitem__all_values()
|
||||||
|
|
||||||
|
try:
|
||||||
|
getitem = self._obj.__getitem__
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
annotation = DirectObjectAccess(self._inference_state, getitem).get_return_annotation()
|
||||||
|
if annotation is not None:
|
||||||
|
return [annotation]
|
||||||
|
return None
|
||||||
|
|
||||||
|
def py__simple_getitem__(self, index, *, safe=True):
|
||||||
|
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
|
||||||
|
|
||||||
return self._create_access_path(self._obj[index])
|
return self._create_access_path(self._obj[index])
|
||||||
|
|
||||||
def py__iter__list(self):
|
def py__iter__list(self):
|
||||||
if not hasattr(self._obj, '__getitem__'):
|
try:
|
||||||
|
iter_method = self._obj.__iter__
|
||||||
|
except AttributeError:
|
||||||
return None
|
return None
|
||||||
|
else:
|
||||||
|
p = DirectObjectAccess(self._inference_state, iter_method).get_return_annotation()
|
||||||
|
if p is not None:
|
||||||
|
return [p]
|
||||||
|
|
||||||
if type(self._obj) not in ALLOWED_GETITEM_TYPES:
|
if 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.
|
||||||
@@ -306,33 +329,37 @@ class DirectObjectAccess:
|
|||||||
except TypeError:
|
except TypeError:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def is_allowed_getattr(self, name, unsafe=False):
|
def is_allowed_getattr(self, name, safe=True) -> Tuple[bool, bool, Optional[AccessPath]]:
|
||||||
# TODO this API is ugly.
|
# TODO this API is ugly.
|
||||||
if unsafe:
|
|
||||||
# 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:
|
||||||
@@ -361,7 +388,7 @@ class DirectObjectAccess:
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
if module is not None:
|
if module is not None and isinstance(module, str):
|
||||||
try:
|
try:
|
||||||
__import__(module)
|
__import__(module)
|
||||||
# For some modules like _sqlite3, the __module__ for classes is
|
# For some modules like _sqlite3, the __module__ for classes is
|
||||||
@@ -492,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,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -187,7 +187,7 @@ def _find_syntax_node_name(inference_state, python_object):
|
|||||||
try:
|
try:
|
||||||
python_object = _get_object_to_check(python_object)
|
python_object = _get_object_to_check(python_object)
|
||||||
path = inspect.getsourcefile(python_object)
|
path = inspect.getsourcefile(python_object)
|
||||||
except TypeError:
|
except (OSError, TypeError):
|
||||||
# The type might not be known (e.g. class_with_dict.__weakref__)
|
# The type might not be known (e.g. class_with_dict.__weakref__)
|
||||||
return None
|
return None
|
||||||
path = None if path is None else Path(path)
|
path = None if path is None else Path(path)
|
||||||
@@ -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,8 +5,26 @@ 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 os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import queue
|
import queue
|
||||||
@@ -15,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
|
||||||
@@ -24,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
|
||||||
@@ -82,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)
|
||||||
@@ -115,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)
|
||||||
|
|
||||||
@@ -127,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,
|
||||||
@@ -163,12 +222,23 @@ 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):
|
||||||
self._executable = executable
|
self._executable = executable
|
||||||
self._env_vars = env_vars
|
self._env_vars = env_vars
|
||||||
self._inference_state_deletion_queue = queue.deque()
|
self._inference_state_deletion_queue = collections.deque()
|
||||||
self._cleanup_callable = lambda: None
|
self._cleanup_callable = lambda: None
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
@@ -212,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, (), {})
|
||||||
@@ -271,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
|
||||||
@@ -307,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)
|
||||||
@@ -347,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_
|
||||||
|
|||||||
@@ -21,11 +21,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,12 +2,11 @@ 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 zipimport import zipimporter
|
from zipfile import ZipFile
|
||||||
|
from zipimport import zipimporter, ZipImportError
|
||||||
from importlib.machinery import all_suffixes
|
from importlib.machinery import all_suffixes
|
||||||
|
|
||||||
from jedi._compatibility import cast_path
|
|
||||||
from jedi.inference.compiled import access
|
from jedi.inference.compiled import access
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi import parser_utils
|
from jedi import parser_utils
|
||||||
@@ -15,7 +14,7 @@ from jedi.file_io import KnownContentFileIO, ZipFileIO
|
|||||||
|
|
||||||
|
|
||||||
def get_sys_path():
|
def get_sys_path():
|
||||||
return list(map(cast_path, sys.path))
|
return sys.path
|
||||||
|
|
||||||
|
|
||||||
def load_module(inference_state, **kwargs):
|
def load_module(inference_state, **kwargs):
|
||||||
@@ -93,15 +92,22 @@ def _iter_module_names(inference_state, paths):
|
|||||||
# Python modules/packages
|
# Python modules/packages
|
||||||
for path in paths:
|
for path in paths:
|
||||||
try:
|
try:
|
||||||
dirs = os.scandir(path)
|
dir_entries = ((entry.name, entry.is_dir()) for entry in os.scandir(path))
|
||||||
except OSError:
|
except OSError:
|
||||||
# The file might not exist or reading it might lead to an error.
|
try:
|
||||||
debug.warning("Not possible to list directory: %s", path)
|
zip_import_info = zipimporter(path)
|
||||||
continue
|
# Unfortunately, there is no public way to access zipimporter's
|
||||||
for dir_entry in dirs:
|
# private _files member. We therefore have to use a
|
||||||
name = dir_entry.name
|
# custom function to iterate over the files.
|
||||||
|
dir_entries = _zip_list_subdirectory(
|
||||||
|
zip_import_info.archive, zip_import_info.prefix)
|
||||||
|
except ZipImportError:
|
||||||
|
# The file might not exist or reading it might lead to an error.
|
||||||
|
debug.warning("Not possible to list directory: %s", path)
|
||||||
|
continue
|
||||||
|
for name, is_dir in dir_entries:
|
||||||
# First Namespaces then modules/stubs
|
# First Namespaces then modules/stubs
|
||||||
if dir_entry.is_dir():
|
if is_dir:
|
||||||
# pycache is obviously not an interesting namespace. Also the
|
# pycache is obviously not an interesting namespace. Also the
|
||||||
# name must be a valid identifier.
|
# name must be a valid identifier.
|
||||||
if name != '__pycache__' and name.isidentifier():
|
if name != '__pycache__' and name.isidentifier():
|
||||||
@@ -144,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
|
||||||
@@ -156,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.
|
||||||
@@ -190,7 +199,7 @@ def _from_loader(loader, string):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
return None, is_package
|
return None, is_package
|
||||||
else:
|
else:
|
||||||
module_path = cast_path(get_filename(string))
|
module_path = get_filename(string)
|
||||||
|
|
||||||
# To avoid unicode and read bytes, "overwrite" loader.get_source if
|
# To avoid unicode and read bytes, "overwrite" loader.get_source if
|
||||||
# possible.
|
# possible.
|
||||||
@@ -212,7 +221,7 @@ def _from_loader(loader, string):
|
|||||||
if code is None:
|
if code is None:
|
||||||
return None, is_package
|
return None, is_package
|
||||||
if isinstance(loader, zipimporter):
|
if isinstance(loader, zipimporter):
|
||||||
return ZipFileIO(module_path, code, Path(cast_path(loader.archive))), is_package
|
return ZipFileIO(module_path, code, Path(loader.archive)), is_package
|
||||||
|
|
||||||
return KnownContentFileIO(module_path, code), is_package
|
return KnownContentFileIO(module_path, code), is_package
|
||||||
|
|
||||||
@@ -230,6 +239,17 @@ def _get_source(loader, fullname):
|
|||||||
name=fullname)
|
name=fullname)
|
||||||
|
|
||||||
|
|
||||||
|
def _zip_list_subdirectory(zip_path, zip_subdir_path):
|
||||||
|
zip_file = ZipFile(zip_path)
|
||||||
|
zip_subdir_path = Path(zip_subdir_path)
|
||||||
|
zip_content_file_paths = zip_file.namelist()
|
||||||
|
for raw_file_name in zip_content_file_paths:
|
||||||
|
file_path = Path(raw_file_name)
|
||||||
|
if file_path.parent == zip_subdir_path:
|
||||||
|
file_path = file_path.relative_to(zip_subdir_path)
|
||||||
|
yield file_path.name, raw_file_name.endswith("/")
|
||||||
|
|
||||||
|
|
||||||
class ImplicitNSInfo:
|
class ImplicitNSInfo:
|
||||||
"""Stores information returned from an implicit namespace spec"""
|
"""Stores information returned from an implicit namespace spec"""
|
||||||
def __init__(self, name, paths):
|
def __init__(self, name, paths):
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ import re
|
|||||||
from functools import partial
|
from functools import partial
|
||||||
from inspect import Parameter
|
from inspect import Parameter
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi.inference.utils import to_list
|
from jedi.inference.utils import to_list
|
||||||
from jedi._compatibility import cast_path
|
|
||||||
from jedi.cache import memoize_method
|
from jedi.cache import memoize_method
|
||||||
from jedi.inference.filters import AbstractFilter
|
from jedi.inference.filters import AbstractFilter
|
||||||
from jedi.inference.names import AbstractNameDefinition, ValueNameMixin, \
|
from jedi.inference.names import AbstractNameDefinition, ValueNameMixin, \
|
||||||
@@ -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,11 +162,14 @@ 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:
|
||||||
return NO_VALUES
|
return super().py__simple_getitem__(index)
|
||||||
|
|
||||||
return ValueSet([create_from_access_path(self.inference_state, access)])
|
return ValueSet([create_from_access_path(self.inference_state, access)])
|
||||||
|
|
||||||
@@ -293,10 +295,7 @@ class CompiledModule(CompiledValue):
|
|||||||
return CompiledModuleContext(self)
|
return CompiledModuleContext(self)
|
||||||
|
|
||||||
def py__path__(self):
|
def py__path__(self):
|
||||||
paths = self.access_handle.py__path__()
|
return self.access_handle.py__path__()
|
||||||
if paths is None:
|
|
||||||
return None
|
|
||||||
return map(cast_path, paths)
|
|
||||||
|
|
||||||
def is_package(self):
|
def is_package(self):
|
||||||
return self.py__path__() is not None
|
return self.py__path__() is not None
|
||||||
@@ -309,19 +308,17 @@ class CompiledModule(CompiledValue):
|
|||||||
return ()
|
return ()
|
||||||
return tuple(name.split('.'))
|
return tuple(name.split('.'))
|
||||||
|
|
||||||
def py__file__(self):
|
def py__file__(self) -> Optional[Path]:
|
||||||
path = cast_path(self.access_handle.py__file__())
|
return self.access_handle.py__file__() # type: ignore[no-any-return]
|
||||||
if path is None:
|
|
||||||
return None
|
|
||||||
return Path(path)
|
|
||||||
|
|
||||||
|
|
||||||
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__()
|
||||||
@@ -348,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):
|
||||||
@@ -438,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, unsafe: access_handle.is_allowed_getattr(name, unsafe),
|
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
|
||||||
)
|
)
|
||||||
@@ -449,36 +452,40 @@ 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,
|
||||||
unsafe=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
|
||||||
names = []
|
names = []
|
||||||
needs_type_completions, dir_infos = self.compiled_value.access_handle.get_dir_infos()
|
needs_type_completions, dir_infos = self.compiled_value.access_handle.get_dir_infos()
|
||||||
# We could use `unsafe` here as well, especially as a parameter to
|
# We could use `safe=False` here as well, especially as a parameter to
|
||||||
# get_dir_infos. But this would lead to a lot of property executions
|
# get_dir_infos. But this would lead to a lot of property executions
|
||||||
# that are probably not wanted. The drawback for this is that we
|
# that are probably not wanted. The drawback for this is that we
|
||||||
# have a different name for `get` and `values`. For `get` we always
|
# have a different name for `get` and `values`. For `get` we always
|
||||||
@@ -486,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, unsafe: dir_infos[name],
|
lambda name: dir_infos[name],
|
||||||
lambda name: name in dir_infos,
|
lambda name: name in dir_infos,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -496,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):
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from parso.tree import search_ancestor
|
from parso.tree import search_ancestor
|
||||||
from parso.python.tree import Name
|
from parso.python.tree import Name
|
||||||
@@ -255,8 +257,7 @@ class TreeContextMixin:
|
|||||||
if scope_node.type in ('funcdef', 'lambdef', 'classdef'):
|
if scope_node.type in ('funcdef', 'lambdef', 'classdef'):
|
||||||
return self.create_value(scope_node).as_context()
|
return self.create_value(scope_node).as_context()
|
||||||
elif scope_node.type in ('comp_for', 'sync_comp_for'):
|
elif scope_node.type in ('comp_for', 'sync_comp_for'):
|
||||||
parent_scope = parser_utils.get_parent_scope(scope_node)
|
parent_context = from_scope_node(parent_scope(scope_node.parent))
|
||||||
parent_context = from_scope_node(parent_scope)
|
|
||||||
if node.start_pos >= scope_node.children[-1].start_pos:
|
if node.start_pos >= scope_node.children[-1].start_pos:
|
||||||
return parent_context
|
return parent_context
|
||||||
return CompForContext(parent_context, scope_node)
|
return CompForContext(parent_context, scope_node)
|
||||||
@@ -308,8 +309,8 @@ class FunctionContext(TreeContextMixin, ValueContext):
|
|||||||
|
|
||||||
|
|
||||||
class ModuleContext(TreeContextMixin, ValueContext):
|
class ModuleContext(TreeContextMixin, ValueContext):
|
||||||
def py__file__(self):
|
def py__file__(self) -> Optional[Path]:
|
||||||
return self._value.py__file__()
|
return self._value.py__file__() # type: ignore[no-any-return]
|
||||||
|
|
||||||
def get_filters(self, until_position=None, origin_scope=None):
|
def get_filters(self, until_position=None, origin_scope=None):
|
||||||
filters = self._value.get_filters(origin_scope)
|
filters = self._value.get_filters(origin_scope)
|
||||||
@@ -326,7 +327,7 @@ class ModuleContext(TreeContextMixin, ValueContext):
|
|||||||
yield from filters
|
yield from filters
|
||||||
|
|
||||||
def get_global_filter(self):
|
def get_global_filter(self):
|
||||||
return GlobalNameFilter(self, self.tree_node)
|
return GlobalNameFilter(self)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def string_names(self):
|
def string_names(self):
|
||||||
@@ -356,8 +357,8 @@ class NamespaceContext(TreeContextMixin, ValueContext):
|
|||||||
def string_names(self):
|
def string_names(self):
|
||||||
return self._value.string_names
|
return self._value.string_names
|
||||||
|
|
||||||
def py__file__(self):
|
def py__file__(self) -> Optional[Path]:
|
||||||
return self._value.py__file__()
|
return self._value.py__file__() # type: ignore[no-any-return]
|
||||||
|
|
||||||
|
|
||||||
class ClassContext(TreeContextMixin, ValueContext):
|
class ClassContext(TreeContextMixin, ValueContext):
|
||||||
@@ -406,8 +407,8 @@ class CompiledModuleContext(CompiledContext):
|
|||||||
def string_names(self):
|
def string_names(self):
|
||||||
return self._value.string_names
|
return self._value.string_names
|
||||||
|
|
||||||
def py__file__(self):
|
def py__file__(self) -> Optional[Path]:
|
||||||
return self._value.py__file__()
|
return self._value.py__file__() # type: ignore[no-any-return]
|
||||||
|
|
||||||
|
|
||||||
def _get_global_filters_for_name(context, name_or_none, position):
|
def _get_global_filters_for_name(context, name_or_none, position):
|
||||||
|
|||||||
21
jedi/inference/docstring_utils.py
Normal file
21
jedi/inference/docstring_utils.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
from jedi.inference.value import ModuleValue
|
||||||
|
from jedi.inference.context import ModuleContext
|
||||||
|
|
||||||
|
|
||||||
|
class DocstringModule(ModuleValue):
|
||||||
|
def __init__(self, in_module_context, **kwargs):
|
||||||
|
super().__init__(**kwargs)
|
||||||
|
self._in_module_context = in_module_context
|
||||||
|
|
||||||
|
def _as_context(self):
|
||||||
|
return DocstringModuleContext(self, self._in_module_context)
|
||||||
|
|
||||||
|
|
||||||
|
class DocstringModuleContext(ModuleContext):
|
||||||
|
def __init__(self, module_value, in_module_context):
|
||||||
|
super().__init__(module_value)
|
||||||
|
self._in_module_context = in_module_context
|
||||||
|
|
||||||
|
def get_filters(self, origin_scope=None, until_position=None):
|
||||||
|
yield from super().get_filters(until_position=until_position)
|
||||||
|
yield from self._in_module_context.get_filters()
|
||||||
@@ -17,12 +17,10 @@ annotations.
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
import warnings
|
import warnings
|
||||||
from textwrap import dedent
|
|
||||||
|
|
||||||
from parso import parse, ParserSyntaxError
|
from parso import parse, ParserSyntaxError
|
||||||
|
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi.common import indent_block
|
|
||||||
from jedi.inference.cache import inference_state_method_cache
|
from jedi.inference.cache import inference_state_method_cache
|
||||||
from jedi.inference.base_value import iterator_to_value_set, ValueSet, \
|
from jedi.inference.base_value import iterator_to_value_set, ValueSet, \
|
||||||
NO_VALUES
|
NO_VALUES
|
||||||
@@ -182,52 +180,40 @@ def _strip_rst_role(type_str):
|
|||||||
|
|
||||||
|
|
||||||
def _infer_for_statement_string(module_context, string):
|
def _infer_for_statement_string(module_context, string):
|
||||||
code = dedent("""
|
|
||||||
def pseudo_docstring_stuff():
|
|
||||||
'''
|
|
||||||
Create a pseudo function for docstring statements.
|
|
||||||
Need this docstring so that if the below part is not valid Python this
|
|
||||||
is still a function.
|
|
||||||
'''
|
|
||||||
{}
|
|
||||||
""")
|
|
||||||
if string is None:
|
if string is None:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
for element in re.findall(r'((?:\w+\.)*\w+)\.', string):
|
potential_imports = re.findall(r'((?:\w+\.)*\w+)\.', string)
|
||||||
# Try to import module part in dotted name.
|
# Try to import module part in dotted name.
|
||||||
# (e.g., 'threading' in 'threading.Thread').
|
# (e.g., 'threading' in 'threading.Thread').
|
||||||
string = 'import %s\n' % element + string
|
imports = "\n".join(f"import {p}" for p in potential_imports)
|
||||||
|
string = f'{imports}\n{string}'
|
||||||
|
|
||||||
debug.dbg('Parse docstring code %s', string, color='BLUE')
|
debug.dbg('Parse docstring code %s', string, color='BLUE')
|
||||||
grammar = module_context.inference_state.grammar
|
grammar = module_context.inference_state.grammar
|
||||||
try:
|
try:
|
||||||
module = grammar.parse(code.format(indent_block(string)), error_recovery=False)
|
module = grammar.parse(string, error_recovery=False)
|
||||||
except ParserSyntaxError:
|
except ParserSyntaxError:
|
||||||
return []
|
return []
|
||||||
try:
|
try:
|
||||||
funcdef = next(module.iter_funcdefs())
|
# It's not the last item, because that's an end marker.
|
||||||
# First pick suite, then simple_stmt and then the node,
|
stmt = module.children[-2]
|
||||||
# which is also not the last item, because there's a newline.
|
|
||||||
stmt = funcdef.children[-1].children[-1].children[-2]
|
|
||||||
except (AttributeError, IndexError):
|
except (AttributeError, IndexError):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
if stmt.type not in ('name', 'atom', 'atom_expr'):
|
if stmt.type not in ('name', 'atom', 'atom_expr'):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
from jedi.inference.value import FunctionValue
|
# Here we basically use a fake module that also uses the filters in
|
||||||
function_value = FunctionValue(
|
# the actual module.
|
||||||
module_context.inference_state,
|
from jedi.inference.docstring_utils import DocstringModule
|
||||||
module_context,
|
m = DocstringModule(
|
||||||
funcdef
|
in_module_context=module_context,
|
||||||
|
inference_state=module_context.inference_state,
|
||||||
|
module_node=module,
|
||||||
|
code_lines=[],
|
||||||
)
|
)
|
||||||
func_execution_context = function_value.as_context()
|
return list(_execute_types_in_stmt(m.as_context(), stmt))
|
||||||
# Use the module of the param.
|
|
||||||
# TODO this module is not the module of the param in case of a function
|
|
||||||
# call. In that case it's the module of the function call.
|
|
||||||
# stuffed with content from a function call.
|
|
||||||
return list(_execute_types_in_stmt(func_execution_context, stmt))
|
|
||||||
|
|
||||||
|
|
||||||
def _execute_types_in_stmt(module_context, stmt):
|
def _execute_types_in_stmt(module_context, stmt):
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ from parso.python.tree import Name, UsedNamesMapping
|
|||||||
from jedi.inference import flow_analysis
|
from jedi.inference import flow_analysis
|
||||||
from jedi.inference.base_value import ValueSet, ValueWrapper, \
|
from jedi.inference.base_value import ValueSet, ValueWrapper, \
|
||||||
LazyValueWrapper
|
LazyValueWrapper
|
||||||
from jedi.parser_utils import get_cached_parent_scope
|
from jedi.parser_utils import get_cached_parent_scope, get_parso_cache_node
|
||||||
from jedi.inference.utils import to_list
|
from jedi.inference.utils import to_list
|
||||||
from jedi.inference.names import TreeNameDefinition, ParamName, \
|
from jedi.inference.names import TreeNameDefinition, ParamName, \
|
||||||
AnonymousParamName, AbstractNameDefinition, NameWrapper
|
AnonymousParamName, AbstractNameDefinition, NameWrapper
|
||||||
@@ -54,11 +54,15 @@ class FilterWrapper:
|
|||||||
return self.wrap_names(self._wrapped_filter.values())
|
return self.wrap_names(self._wrapped_filter.values())
|
||||||
|
|
||||||
|
|
||||||
def _get_definition_names(used_names, name_key):
|
def _get_definition_names(parso_cache_node, used_names, name_key):
|
||||||
|
if parso_cache_node is None:
|
||||||
|
names = used_names.get(name_key, ())
|
||||||
|
return tuple(name for name in names if name.is_definition(include_setitem=True))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for_module = _definition_name_cache[used_names]
|
for_module = _definition_name_cache[parso_cache_node]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
for_module = _definition_name_cache[used_names] = {}
|
for_module = _definition_name_cache[parso_cache_node] = {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return for_module[name_key]
|
return for_module[name_key]
|
||||||
@@ -70,18 +74,40 @@ def _get_definition_names(used_names, name_key):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
class AbstractUsedNamesFilter(AbstractFilter):
|
class _AbstractUsedNamesFilter(AbstractFilter):
|
||||||
name_class = TreeNameDefinition
|
name_class = TreeNameDefinition
|
||||||
|
|
||||||
def __init__(self, parent_context, parser_scope):
|
def __init__(self, parent_context, node_context=None):
|
||||||
self._parser_scope = parser_scope
|
if node_context is None:
|
||||||
self._module_node = self._parser_scope.get_root_node()
|
node_context = parent_context
|
||||||
self._used_names = self._module_node.get_used_names()
|
self._node_context = node_context
|
||||||
|
self._parser_scope = node_context.tree_node
|
||||||
|
module_context = node_context.get_root_context()
|
||||||
|
# It is quite hacky that we have to use that. This is for caching
|
||||||
|
# certain things with a WeakKeyDictionary. However, parso intentionally
|
||||||
|
# uses slots (to save memory) and therefore we end up with having to
|
||||||
|
# have a weak reference to the object that caches the tree.
|
||||||
|
#
|
||||||
|
# Previously we have tried to solve this by using a weak reference onto
|
||||||
|
# used_names. However that also does not work, because it has a
|
||||||
|
# reference from the module, which itself is referenced by any node
|
||||||
|
# through parents.
|
||||||
|
path = module_context.py__file__()
|
||||||
|
if path is None:
|
||||||
|
# If the path is None, there is no guarantee that parso caches it.
|
||||||
|
self._parso_cache_node = None
|
||||||
|
else:
|
||||||
|
self._parso_cache_node = get_parso_cache_node(
|
||||||
|
module_context.inference_state.latest_grammar
|
||||||
|
if module_context.is_stub() else module_context.inference_state.grammar,
|
||||||
|
path
|
||||||
|
)
|
||||||
|
self._used_names = module_context.tree_node.get_used_names()
|
||||||
self.parent_context = parent_context
|
self.parent_context = parent_context
|
||||||
|
|
||||||
def get(self, name):
|
def get(self, name):
|
||||||
return self._convert_names(self._filter(
|
return self._convert_names(self._filter(
|
||||||
_get_definition_names(self._used_names, name),
|
_get_definition_names(self._parso_cache_node, self._used_names, name),
|
||||||
))
|
))
|
||||||
|
|
||||||
def _convert_names(self, names):
|
def _convert_names(self, names):
|
||||||
@@ -92,7 +118,7 @@ class AbstractUsedNamesFilter(AbstractFilter):
|
|||||||
name
|
name
|
||||||
for name_key in self._used_names
|
for name_key in self._used_names
|
||||||
for name in self._filter(
|
for name in self._filter(
|
||||||
_get_definition_names(self._used_names, name_key),
|
_get_definition_names(self._parso_cache_node, self._used_names, name_key),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -100,7 +126,7 @@ class AbstractUsedNamesFilter(AbstractFilter):
|
|||||||
return '<%s: %s>' % (self.__class__.__name__, self.parent_context)
|
return '<%s: %s>' % (self.__class__.__name__, self.parent_context)
|
||||||
|
|
||||||
|
|
||||||
class ParserTreeFilter(AbstractUsedNamesFilter):
|
class ParserTreeFilter(_AbstractUsedNamesFilter):
|
||||||
def __init__(self, parent_context, node_context=None, until_position=None,
|
def __init__(self, parent_context, node_context=None, until_position=None,
|
||||||
origin_scope=None):
|
origin_scope=None):
|
||||||
"""
|
"""
|
||||||
@@ -109,10 +135,7 @@ class ParserTreeFilter(AbstractUsedNamesFilter):
|
|||||||
value, but for some type inference it's important to have a local
|
value, but for some type inference it's important to have a local
|
||||||
value of the other classes.
|
value of the other classes.
|
||||||
"""
|
"""
|
||||||
if node_context is None:
|
super().__init__(parent_context, node_context)
|
||||||
node_context = parent_context
|
|
||||||
super().__init__(parent_context, node_context.tree_node)
|
|
||||||
self._node_context = node_context
|
|
||||||
self._origin_scope = origin_scope
|
self._origin_scope = origin_scope
|
||||||
self._until_position = until_position
|
self._until_position = until_position
|
||||||
|
|
||||||
@@ -126,7 +149,7 @@ class ParserTreeFilter(AbstractUsedNamesFilter):
|
|||||||
if parent.type == 'trailer':
|
if parent.type == 'trailer':
|
||||||
return False
|
return False
|
||||||
base_node = parent if parent.type in ('classdef', 'funcdef') else name
|
base_node = parent if parent.type in ('classdef', 'funcdef') else name
|
||||||
return get_cached_parent_scope(self._used_names, base_node) == self._parser_scope
|
return get_cached_parent_scope(self._parso_cache_node, base_node) == self._parser_scope
|
||||||
|
|
||||||
def _check_flows(self, names):
|
def _check_flows(self, names):
|
||||||
for name in sorted(names, key=lambda name: name.start_pos, reverse=True):
|
for name in sorted(names, key=lambda name: name.start_pos, reverse=True):
|
||||||
@@ -182,7 +205,7 @@ class AnonymousFunctionExecutionFilter(_FunctionExecutionFilter):
|
|||||||
return AnonymousParamName(self._function_value, name)
|
return AnonymousParamName(self._function_value, name)
|
||||||
|
|
||||||
|
|
||||||
class GlobalNameFilter(AbstractUsedNamesFilter):
|
class GlobalNameFilter(_AbstractUsedNamesFilter):
|
||||||
def get(self, name):
|
def get(self, name):
|
||||||
try:
|
try:
|
||||||
names = self._used_names[name]
|
names = self._used_names[name]
|
||||||
|
|||||||
@@ -196,13 +196,43 @@ def py__annotations__(funcdef):
|
|||||||
return dct
|
return dct
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_forward_references(context, all_annotations):
|
||||||
|
def resolve(node):
|
||||||
|
if node is None or node.type != 'string':
|
||||||
|
return node
|
||||||
|
|
||||||
|
node = _get_forward_reference_node(
|
||||||
|
context,
|
||||||
|
context.inference_state.compiled_subprocess.safe_literal_eval(
|
||||||
|
node.value,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
if node is None:
|
||||||
|
# There was a string, but it's not a valid annotation
|
||||||
|
return None
|
||||||
|
|
||||||
|
# The forward reference tree has an additional root node ('eval_input')
|
||||||
|
# that we don't want. Extract the node we do want, that is equivalent to
|
||||||
|
# the nodes returned by `py__annotations__` for a non-quoted node.
|
||||||
|
node = node.children[0]
|
||||||
|
|
||||||
|
return node
|
||||||
|
|
||||||
|
return {name: resolve(node) for name, node in all_annotations.items()}
|
||||||
|
|
||||||
|
|
||||||
@inference_state_method_cache()
|
@inference_state_method_cache()
|
||||||
def infer_return_types(function, arguments):
|
def infer_return_types(function, arguments):
|
||||||
"""
|
"""
|
||||||
Infers the type of a function's return value,
|
Infers the type of a function's return value,
|
||||||
according to type annotations.
|
according to type annotations.
|
||||||
"""
|
"""
|
||||||
all_annotations = py__annotations__(function.tree_node)
|
context = function.get_default_param_context()
|
||||||
|
all_annotations = resolve_forward_references(
|
||||||
|
context,
|
||||||
|
py__annotations__(function.tree_node),
|
||||||
|
)
|
||||||
annotation = all_annotations.get("return", None)
|
annotation = all_annotations.get("return", None)
|
||||||
if annotation is None:
|
if annotation is None:
|
||||||
# If there is no Python 3-type annotation, look for an annotation
|
# If there is no Python 3-type annotation, look for an annotation
|
||||||
@@ -217,11 +247,10 @@ def infer_return_types(function, arguments):
|
|||||||
return NO_VALUES
|
return NO_VALUES
|
||||||
|
|
||||||
return _infer_annotation_string(
|
return _infer_annotation_string(
|
||||||
function.get_default_param_context(),
|
context,
|
||||||
match.group(1).strip()
|
match.group(1).strip()
|
||||||
).execute_annotation()
|
).execute_annotation()
|
||||||
|
|
||||||
context = function.get_default_param_context()
|
|
||||||
unknown_type_vars = find_unknown_type_vars(context, annotation)
|
unknown_type_vars = find_unknown_type_vars(context, annotation)
|
||||||
annotation_values = infer_annotation(context, annotation)
|
annotation_values = infer_annotation(context, annotation)
|
||||||
if not unknown_type_vars:
|
if not unknown_type_vars:
|
||||||
@@ -373,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]
|
||||||
|
|||||||
@@ -146,6 +146,9 @@ class DefineGenericBaseClass(LazyValueWrapper):
|
|||||||
) for class_set1, class_set2 in zip(given_params1, given_params2)
|
) for class_set1, class_set2 in zip(given_params1, given_params2)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def get_signatures(self):
|
||||||
|
return []
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<%s: %s%s>' % (
|
return '<%s: %s%s>' % (
|
||||||
self.__class__.__name__,
|
self.__class__.__name__,
|
||||||
@@ -380,6 +383,9 @@ class BaseTypingValue(LazyValueWrapper):
|
|||||||
def _get_wrapped_value(self):
|
def _get_wrapped_value(self):
|
||||||
return _PseudoTreeNameClass(self.parent_context, self._tree_name)
|
return _PseudoTreeNameClass(self.parent_context, self._tree_name)
|
||||||
|
|
||||||
|
def get_signatures(self):
|
||||||
|
return self._wrapped_value.get_signatures()
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '%s(%s)' % (self.__class__.__name__, self._tree_name.value)
|
return '%s(%s)' % (self.__class__.__name__, self._tree_name.value)
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ from jedi.inference.base_value import ValueSet, NO_VALUES, ValueWrapper
|
|||||||
from jedi.inference.gradual.base import BaseTypingValue
|
from jedi.inference.gradual.base import BaseTypingValue
|
||||||
|
|
||||||
|
|
||||||
class TypeVarClass(BaseTypingValue):
|
class TypeVarClass(ValueWrapper):
|
||||||
def py__call__(self, arguments):
|
def py__call__(self, arguments):
|
||||||
unpacked = arguments.unpack()
|
unpacked = arguments.unpack()
|
||||||
|
|
||||||
@@ -17,9 +17,9 @@ class TypeVarClass(BaseTypingValue):
|
|||||||
return ValueSet([TypeVar.create_cached(
|
return ValueSet([TypeVar.create_cached(
|
||||||
self.inference_state,
|
self.inference_state,
|
||||||
self.parent_context,
|
self.parent_context,
|
||||||
self._tree_name,
|
tree_name=self.tree_node.name,
|
||||||
var_name,
|
var_name=var_name,
|
||||||
unpacked
|
unpacked_args=unpacked,
|
||||||
)])
|
)])
|
||||||
|
|
||||||
def _find_string_name(self, lazy_value):
|
def _find_string_name(self, lazy_value):
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ from pathlib import Path
|
|||||||
|
|
||||||
from jedi import settings
|
from jedi import settings
|
||||||
from jedi.file_io import FileIO
|
from jedi.file_io import FileIO
|
||||||
from jedi._compatibility import cast_path
|
|
||||||
from jedi.parser_utils import get_cached_code_lines
|
from jedi.parser_utils import get_cached_code_lines
|
||||||
from jedi.inference.base_value import ValueSet, NO_VALUES
|
from jedi.inference.base_value import ValueSet, NO_VALUES
|
||||||
from jedi.inference.gradual.stub_value import TypingModuleWrapper, StubModuleValue
|
from jedi.inference.gradual.stub_value import TypingModuleWrapper, StubModuleValue
|
||||||
@@ -44,7 +43,6 @@ def _create_stub_map(directory_path_info):
|
|||||||
return
|
return
|
||||||
|
|
||||||
for entry in listed:
|
for entry in listed:
|
||||||
entry = cast_path(entry)
|
|
||||||
path = os.path.join(directory_path_info.path, entry)
|
path = os.path.join(directory_path_info.path, entry)
|
||||||
if os.path.isdir(path):
|
if os.path.isdir(path):
|
||||||
init = os.path.join(path, '__init__.pyi')
|
init = os.path.join(path, '__init__.pyi')
|
||||||
@@ -120,7 +118,7 @@ def import_module_decorator(func):
|
|||||||
)
|
)
|
||||||
inference_state.module_cache.add(import_names, python_value_set)
|
inference_state.module_cache.add(import_names, python_value_set)
|
||||||
|
|
||||||
if not prefer_stubs:
|
if not prefer_stubs or import_names[0] in settings.auto_import_modules:
|
||||||
return python_value_set
|
return python_value_set
|
||||||
|
|
||||||
stub = try_to_load_stub_cached(inference_state, import_names, python_value_set,
|
stub = try_to_load_stub_cached(inference_state, import_names, python_value_set,
|
||||||
@@ -169,7 +167,6 @@ def _try_to_load_stub(inference_state, import_names, python_value_set,
|
|||||||
if len(import_names) == 1:
|
if len(import_names) == 1:
|
||||||
# foo-stubs
|
# foo-stubs
|
||||||
for p in sys_path:
|
for p in sys_path:
|
||||||
p = cast_path(p)
|
|
||||||
init = os.path.join(p, *import_names) + '-stubs' + os.path.sep + '__init__.pyi'
|
init = os.path.join(p, *import_names) + '-stubs' + os.path.sep + '__init__.pyi'
|
||||||
m = _try_to_load_stub_from_file(
|
m = _try_to_load_stub_from_file(
|
||||||
inference_state,
|
inference_state,
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import itertools
|
|||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi.inference.compiled import builtin_from_name, create_simple_object
|
from jedi.inference.compiled import builtin_from_name, create_simple_object
|
||||||
from jedi.inference.base_value import ValueSet, NO_VALUES, Value, \
|
from jedi.inference.base_value import ValueSet, NO_VALUES, Value, \
|
||||||
LazyValueWrapper
|
LazyValueWrapper, ValueWrapper
|
||||||
from jedi.inference.lazy_value import LazyKnownValues
|
from jedi.inference.lazy_value import LazyKnownValues
|
||||||
from jedi.inference.arguments import repack_with_argument_clinic
|
from jedi.inference.arguments import repack_with_argument_clinic
|
||||||
from jedi.inference.filters import FilterWrapper
|
from jedi.inference.filters import FilterWrapper
|
||||||
@@ -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):
|
||||||
@@ -63,8 +63,8 @@ class TypingModuleName(NameWrapper):
|
|||||||
# have any effects there (because it's never executed).
|
# have any effects there (because it's never executed).
|
||||||
return
|
return
|
||||||
elif name == 'TypeVar':
|
elif name == 'TypeVar':
|
||||||
yield TypeVarClass.create_cached(
|
cls, = self._wrapped_name.infer()
|
||||||
inference_state, self.parent_context, self.tree_name)
|
yield TypeVarClass.create_cached(inference_state, cls)
|
||||||
elif name == 'Any':
|
elif name == 'Any':
|
||||||
yield AnyClass.create_cached(
|
yield AnyClass.create_cached(
|
||||||
inference_state, self.parent_context, self.tree_name)
|
inference_state, self.parent_context, self.tree_name)
|
||||||
@@ -76,21 +76,20 @@ class TypingModuleName(NameWrapper):
|
|||||||
yield OverloadFunction.create_cached(
|
yield OverloadFunction.create_cached(
|
||||||
inference_state, self.parent_context, self.tree_name)
|
inference_state, self.parent_context, self.tree_name)
|
||||||
elif name == 'NewType':
|
elif name == 'NewType':
|
||||||
yield NewTypeFunction.create_cached(
|
v, = self._wrapped_name.infer()
|
||||||
inference_state, self.parent_context, self.tree_name)
|
yield NewTypeFunction.create_cached(inference_state, v)
|
||||||
elif name == 'cast':
|
elif name == 'cast':
|
||||||
yield CastFunction.create_cached(
|
cast_fn, = self._wrapped_name.infer()
|
||||||
inference_state, self.parent_context, self.tree_name)
|
yield CastFunction.create_cached(inference_state, cast_fn)
|
||||||
elif name == 'TypedDict':
|
elif name == 'TypedDict':
|
||||||
# TODO doesn't even exist in typeshed/typing.py, yet. But will be
|
# TODO doesn't even exist in typeshed/typing.py, yet. But will be
|
||||||
# added soon.
|
# added soon.
|
||||||
yield TypedDictClass.create_cached(
|
yield TypedDictClass.create_cached(
|
||||||
inference_state, self.parent_context, self.tree_name)
|
inference_state, self.parent_context, self.tree_name)
|
||||||
elif name in ('no_type_check', 'no_type_check_decorator'):
|
|
||||||
# This is not necessary, as long as we are not doing type checking.
|
|
||||||
yield from self._wrapped_name.infer()
|
|
||||||
else:
|
else:
|
||||||
# Everything else shouldn't be relevant for type checking.
|
# Not necessary, as long as we are not doing type checking:
|
||||||
|
# no_type_check & no_type_check_decorator
|
||||||
|
# Everything else shouldn't be relevant...
|
||||||
yield from self._wrapped_name.infer()
|
yield from self._wrapped_name.infer()
|
||||||
|
|
||||||
|
|
||||||
@@ -114,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()
|
||||||
|
|
||||||
@@ -275,6 +274,9 @@ class TypeAlias(LazyValueWrapper):
|
|||||||
def gather_annotation_classes(self):
|
def gather_annotation_classes(self):
|
||||||
return ValueSet([self._get_wrapped_value()])
|
return ValueSet([self._get_wrapped_value()])
|
||||||
|
|
||||||
|
def get_signatures(self):
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
class Callable(BaseTypingInstance):
|
class Callable(BaseTypingInstance):
|
||||||
def py__call__(self, arguments):
|
def py__call__(self, arguments):
|
||||||
@@ -292,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):
|
||||||
@@ -395,7 +400,7 @@ class OverloadFunction(BaseTypingValue):
|
|||||||
return func_value_set
|
return func_value_set
|
||||||
|
|
||||||
|
|
||||||
class NewTypeFunction(BaseTypingValue):
|
class NewTypeFunction(ValueWrapper):
|
||||||
def py__call__(self, arguments):
|
def py__call__(self, arguments):
|
||||||
ordered_args = arguments.unpack()
|
ordered_args = arguments.unpack()
|
||||||
next(ordered_args, (None, None))
|
next(ordered_args, (None, None))
|
||||||
@@ -429,8 +434,11 @@ class NewType(Value):
|
|||||||
from jedi.inference.compiled.value import CompiledValueName
|
from jedi.inference.compiled.value import CompiledValueName
|
||||||
return CompiledValueName(self, 'NewType')
|
return CompiledValueName(self, 'NewType')
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return '<NewType: %s>%s' % (self.tree_node, self._type_value_set)
|
||||||
|
|
||||||
class CastFunction(BaseTypingValue):
|
|
||||||
|
class CastFunction(ValueWrapper):
|
||||||
@repack_with_argument_clinic('type, object, /')
|
@repack_with_argument_clinic('type, object, /')
|
||||||
def py__call__(self, type_value_set, object_value_set):
|
def py__call__(self, type_value_set, object_value_set):
|
||||||
return type_value_set.execute_annotation()
|
return type_value_set.execute_annotation()
|
||||||
|
|||||||
@@ -339,7 +339,7 @@ class Importer:
|
|||||||
values = self.follow()
|
values = self.follow()
|
||||||
for value in values:
|
for value in values:
|
||||||
# Non-modules are not completable.
|
# Non-modules are not completable.
|
||||||
if value.api_type != 'module': # not a module
|
if value.api_type not in ('module', 'namespace'): # not a module
|
||||||
continue
|
continue
|
||||||
if not value.is_compiled():
|
if not value.is_compiled():
|
||||||
# sub_modules_dict is not implemented for compiled modules.
|
# sub_modules_dict is not implemented for compiled modules.
|
||||||
@@ -422,20 +422,13 @@ def import_module(inference_state, import_names, parent_module_value, sys_path):
|
|||||||
# The module might not be a package.
|
# The module might not be a package.
|
||||||
return NO_VALUES
|
return NO_VALUES
|
||||||
|
|
||||||
for path in paths:
|
file_io_or_ns, is_pkg = inference_state.compiled_subprocess.get_module_info(
|
||||||
# At the moment we are only using one path. So this is
|
string=import_names[-1],
|
||||||
# not important to be correct.
|
path=paths,
|
||||||
if not isinstance(path, list):
|
full_name=module_name,
|
||||||
path = [path]
|
is_global_search=False,
|
||||||
file_io_or_ns, is_pkg = inference_state.compiled_subprocess.get_module_info(
|
)
|
||||||
string=import_names[-1],
|
if is_pkg is None:
|
||||||
path=path,
|
|
||||||
full_name=module_name,
|
|
||||||
is_global_search=False,
|
|
||||||
)
|
|
||||||
if is_pkg is not None:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
return NO_VALUES
|
return NO_VALUES
|
||||||
|
|
||||||
if isinstance(file_io_or_ns, ImplicitNSInfo):
|
if isinstance(file_io_or_ns, ImplicitNSInfo):
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
@@ -340,7 +340,13 @@ class TreeNameDefinition(AbstractTreeName):
|
|||||||
@inference_state_method_cache(default='')
|
@inference_state_method_cache(default='')
|
||||||
def py__doc__(self):
|
def py__doc__(self):
|
||||||
api_type = self.api_type
|
api_type = self.api_type
|
||||||
if api_type in ('function', 'class'):
|
if api_type in ('function', 'class', 'property'):
|
||||||
|
if self.parent_context.get_root_context().is_stub():
|
||||||
|
from jedi.inference.gradual.conversion import convert_names
|
||||||
|
names = convert_names([self], prefer_stub_to_compiled=False)
|
||||||
|
if self not in names:
|
||||||
|
return _merge_name_docs(names)
|
||||||
|
|
||||||
# Make sure the names are not TreeNameDefinitions anymore.
|
# Make sure the names are not TreeNameDefinitions anymore.
|
||||||
return clean_scope_docstring(self.tree_name.get_definition())
|
return clean_scope_docstring(self.tree_name.get_definition())
|
||||||
|
|
||||||
@@ -408,6 +414,9 @@ class ParamNameInterface(_ParamMixin):
|
|||||||
return 2
|
return 2
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
def infer_default(self):
|
||||||
|
return NO_VALUES
|
||||||
|
|
||||||
|
|
||||||
class BaseTreeParamName(ParamNameInterface, AbstractTreeName):
|
class BaseTreeParamName(ParamNameInterface, AbstractTreeName):
|
||||||
annotation_node = None
|
annotation_node = None
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ count the function calls.
|
|||||||
Settings
|
Settings
|
||||||
~~~~~~~~~~
|
~~~~~~~~~~
|
||||||
|
|
||||||
Recursion settings are important if you don't want extremly
|
Recursion settings are important if you don't want extremely
|
||||||
recursive python code to go absolutely crazy.
|
recursive python code to go absolutely crazy.
|
||||||
|
|
||||||
The default values are based on experiments while completing the |jedi| library
|
The default values are based on experiments while completing the |jedi| library
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ import re
|
|||||||
from parso import python_bytes_to_unicode
|
from parso import python_bytes_to_unicode
|
||||||
|
|
||||||
from jedi.debug import dbg
|
from jedi.debug import dbg
|
||||||
from jedi.file_io import KnownContentFileIO
|
from jedi.file_io import KnownContentFileIO, FolderIO
|
||||||
from jedi.inference.names import SubModuleName
|
from jedi.inference.names import SubModuleName
|
||||||
from jedi.inference.imports import load_module_from_path
|
from jedi.inference.imports import load_module_from_path
|
||||||
from jedi.inference.filters import ParserTreeFilter
|
from jedi.inference.filters import ParserTreeFilter
|
||||||
from jedi.inference.gradual.conversion import convert_names
|
from jedi.inference.gradual.conversion import convert_names
|
||||||
|
|
||||||
_IGNORE_FOLDERS = ('.tox', '.venv', 'venv', '__pycache__')
|
_IGNORE_FOLDERS = ('.tox', '.venv', '.mypy_cache', 'venv', '__pycache__')
|
||||||
|
|
||||||
_OPENED_FILE_LIMIT = 2000
|
_OPENED_FILE_LIMIT = 2000
|
||||||
"""
|
"""
|
||||||
@@ -127,10 +127,10 @@ def find_references(module_context, tree_name, only_in_module=False):
|
|||||||
|
|
||||||
module_contexts = [module_context]
|
module_contexts = [module_context]
|
||||||
if not only_in_module:
|
if not only_in_module:
|
||||||
module_contexts.extend(
|
for m in set(d.get_root_context() for d in found_names):
|
||||||
m for m in set(d.get_root_context() for d in found_names)
|
if m != module_context and m.tree_node is not None \
|
||||||
if m != module_context and m.tree_node is not None
|
and inf.project.path in m.py__file__().parents:
|
||||||
)
|
module_contexts.append(m)
|
||||||
# For param no search for other modules is necessary.
|
# For param no search for other modules is necessary.
|
||||||
if only_in_module or any(n.api_type == 'param' for n in found_names):
|
if only_in_module or any(n.api_type == 'param' for n in found_names):
|
||||||
potential_modules = module_contexts
|
potential_modules = module_contexts
|
||||||
@@ -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:
|
||||||
@@ -250,6 +265,11 @@ def _find_python_files_in_sys_path(inference_state, module_contexts):
|
|||||||
folder_io = folder_io.get_parent_folder()
|
folder_io = folder_io.get_parent_folder()
|
||||||
|
|
||||||
|
|
||||||
|
def _find_project_modules(inference_state, module_contexts):
|
||||||
|
except_ = [m.py__file__() for m in module_contexts]
|
||||||
|
yield from recurse_find_python_files(FolderIO(inference_state.project.path), except_)
|
||||||
|
|
||||||
|
|
||||||
def get_module_contexts_containing_name(inference_state, module_contexts, name,
|
def get_module_contexts_containing_name(inference_state, module_contexts, name,
|
||||||
limit_reduction=1):
|
limit_reduction=1):
|
||||||
"""
|
"""
|
||||||
@@ -269,17 +289,21 @@ def get_module_contexts_containing_name(inference_state, module_contexts, name,
|
|||||||
if len(name) <= 2:
|
if len(name) <= 2:
|
||||||
return
|
return
|
||||||
|
|
||||||
file_io_iterator = _find_python_files_in_sys_path(inference_state, module_contexts)
|
# Currently not used, because there's only `scope=project` and `scope=file`
|
||||||
|
# At the moment there is no such thing as `scope=sys.path`.
|
||||||
|
# file_io_iterator = _find_python_files_in_sys_path(inference_state, module_contexts)
|
||||||
|
file_io_iterator = _find_project_modules(inference_state, module_contexts)
|
||||||
yield from search_in_file_ios(inference_state, file_io_iterator, name,
|
yield from search_in_file_ios(inference_state, file_io_iterator, name,
|
||||||
limit_reduction=limit_reduction)
|
limit_reduction=limit_reduction)
|
||||||
|
|
||||||
|
|
||||||
def search_in_file_ios(inference_state, file_io_iterator, name, limit_reduction=1):
|
def search_in_file_ios(inference_state, file_io_iterator, name,
|
||||||
|
limit_reduction=1, complete=False):
|
||||||
parse_limit = _PARSED_FILE_LIMIT / limit_reduction
|
parse_limit = _PARSED_FILE_LIMIT / limit_reduction
|
||||||
open_limit = _OPENED_FILE_LIMIT / limit_reduction
|
open_limit = _OPENED_FILE_LIMIT / limit_reduction
|
||||||
file_io_count = 0
|
file_io_count = 0
|
||||||
parsed_file_count = 0
|
parsed_file_count = 0
|
||||||
regex = re.compile(r'\b' + re.escape(name) + r'\b')
|
regex = re.compile(r'\b' + re.escape(name) + (r'' if complete else r'\b'))
|
||||||
for file_io in file_io_iterator:
|
for file_io in file_io_iterator:
|
||||||
file_io_count += 1
|
file_io_count += 1
|
||||||
m = _check_fs(inference_state, file_io, regex)
|
m = _check_fs(inference_state, file_io, regex)
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ The signature here for bar should be `bar(b, c)` instead of bar(*args).
|
|||||||
"""
|
"""
|
||||||
from inspect import Parameter
|
from inspect import Parameter
|
||||||
|
|
||||||
|
from parso import tree
|
||||||
|
|
||||||
from jedi.inference.utils import to_list
|
from jedi.inference.utils import to_list
|
||||||
from jedi.inference.names import ParamNameWrapper
|
from jedi.inference.names import ParamNameWrapper
|
||||||
from jedi.inference.helpers import is_big_annoying_library
|
from jedi.inference.helpers import is_big_annoying_library
|
||||||
@@ -22,7 +24,11 @@ def _iter_nodes_for_param(param_name):
|
|||||||
from jedi.inference.arguments import TreeArguments
|
from jedi.inference.arguments import TreeArguments
|
||||||
|
|
||||||
execution_context = param_name.parent_context
|
execution_context = param_name.parent_context
|
||||||
function_node = execution_context.tree_node
|
# Walk up the parso tree to get the FunctionNode we want. We use the parso
|
||||||
|
# tree rather than going via the execution context so that we're agnostic of
|
||||||
|
# the specific scope we're evaluating within (i.e: module or function,
|
||||||
|
# etc.).
|
||||||
|
function_node = tree.search_ancestor(param_name.tree_name, 'funcdef', 'lambdef')
|
||||||
module_node = function_node.get_root_node()
|
module_node = function_node.get_root_node()
|
||||||
start = function_node.children[-1].start_pos
|
start = function_node.children[-1].start_pos
|
||||||
end = function_node.children[-1].end_pos
|
end = function_node.children[-1].end_pos
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
@@ -328,8 +329,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 +493,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 +518,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 +647,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 +697,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()
|
||||||
@@ -738,6 +758,13 @@ def tree_name_to_values(inference_state, context, tree_name):
|
|||||||
types = infer_expr_stmt(context, node, tree_name)
|
types = infer_expr_stmt(context, node, tree_name)
|
||||||
elif typ == 'with_stmt':
|
elif typ == 'with_stmt':
|
||||||
value_managers = context.infer_node(node.get_test_node_from_name(tree_name))
|
value_managers = context.infer_node(node.get_test_node_from_name(tree_name))
|
||||||
|
if node.parent.type == 'async_stmt':
|
||||||
|
# In the case of `async with` statements, we need to
|
||||||
|
# first get the coroutine from the `__aenter__` method,
|
||||||
|
# then "unwrap" via the `__await__` method
|
||||||
|
enter_methods = value_managers.py__getattribute__('__aenter__')
|
||||||
|
coro = enter_methods.execute_with_values()
|
||||||
|
return coro.py__await__().py__stop_iteration_returns()
|
||||||
enter_methods = value_managers.py__getattribute__('__enter__')
|
enter_methods = value_managers.py__getattribute__('__enter__')
|
||||||
return enter_methods.execute_with_values()
|
return enter_methods.execute_with_values()
|
||||||
elif typ in ('import_from', 'import_name'):
|
elif typ in ('import_from', 'import_name'):
|
||||||
|
|||||||
@@ -186,7 +186,6 @@ def _get_buildout_script_paths(search_path: Path):
|
|||||||
directory that look like python files.
|
directory that look like python files.
|
||||||
|
|
||||||
:param search_path: absolute path to the module.
|
:param search_path: absolute path to the module.
|
||||||
:type search_path: str
|
|
||||||
"""
|
"""
|
||||||
project_root = _get_parent_dir_with_file(search_path, 'buildout.cfg')
|
project_root = _get_parent_dir_with_file(search_path, 'buildout.cfg')
|
||||||
if not project_root:
|
if not project_root:
|
||||||
@@ -205,7 +204,7 @@ def _get_buildout_script_paths(search_path: Path):
|
|||||||
except (UnicodeDecodeError, IOError) as e:
|
except (UnicodeDecodeError, IOError) as e:
|
||||||
# Probably a binary file; permission error or race cond. because
|
# Probably a binary file; permission error or race cond. because
|
||||||
# file got deleted. Ignore it.
|
# file got deleted. Ignore it.
|
||||||
debug.warning(e)
|
debug.warning(str(e))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -19,3 +19,16 @@ class Decoratee(ValueWrapper):
|
|||||||
Decoratee(v, self._original_value)
|
Decoratee(v, self._original_value)
|
||||||
for v in self._wrapped_value.py__get__(instance, class_value)
|
for v in self._wrapped_value.py__get__(instance, class_value)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def get_signatures(self):
|
||||||
|
signatures = self._wrapped_value.get_signatures()
|
||||||
|
if signatures:
|
||||||
|
return signatures
|
||||||
|
# Fallback to signatures of the original function/class if the
|
||||||
|
# decorator has no signature or it is not inferrable.
|
||||||
|
#
|
||||||
|
# __get__ means that it's a descriptor. In that case we don't return
|
||||||
|
# signatures, because they are usually properties.
|
||||||
|
if not self._wrapped_value.py__getattribute__('__get__'):
|
||||||
|
return self._original_value.get_signatures()
|
||||||
|
return []
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -344,7 +344,8 @@ class BaseFunctionExecutionContext(ValueContext, TreeContextMixin):
|
|||||||
GenericClass(c, TupleGenericManager(generics)) for c in async_classes
|
GenericClass(c, TupleGenericManager(generics)) for c in async_classes
|
||||||
).execute_annotation()
|
).execute_annotation()
|
||||||
else:
|
else:
|
||||||
if self.is_generator():
|
# If there are annotations, prefer them over anything else.
|
||||||
|
if self.is_generator() and not self.infer_annotations():
|
||||||
return ValueSet([iterable.Generator(inference_state, self)])
|
return ValueSet([iterable.Generator(inference_state, self)])
|
||||||
else:
|
else:
|
||||||
return self.get_return_values()
|
return self.get_return_values()
|
||||||
|
|||||||
@@ -121,7 +121,13 @@ class AbstractInstanceValue(Value):
|
|||||||
return [s.bind(self) for s in call_funcs.get_signatures()]
|
return [s.bind(self) for s in call_funcs.get_signatures()]
|
||||||
|
|
||||||
def get_function_slot_names(self, name):
|
def get_function_slot_names(self, name):
|
||||||
# Searches for Python functions in classes.
|
# Python classes don't look at the dictionary of the instance when
|
||||||
|
# looking up `__call__`. This is something that has to do with Python's
|
||||||
|
# internal slot system (note: not __slots__, but C slots).
|
||||||
|
for filter in self.get_filters(include_self_names=False):
|
||||||
|
names = filter.get(name)
|
||||||
|
if names:
|
||||||
|
return names
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def execute_function_slots(self, names, *inferred_args):
|
def execute_function_slots(self, names, *inferred_args):
|
||||||
@@ -133,6 +139,27 @@ class AbstractInstanceValue(Value):
|
|||||||
def get_type_hint(self, add_class_info=True):
|
def get_type_hint(self, add_class_info=True):
|
||||||
return self.py__name__()
|
return self.py__name__()
|
||||||
|
|
||||||
|
def py__getitem__(self, index_value_set, contextualized_node):
|
||||||
|
names = self.get_function_slot_names('__getitem__')
|
||||||
|
if not names:
|
||||||
|
return super().py__getitem__(
|
||||||
|
index_value_set,
|
||||||
|
contextualized_node,
|
||||||
|
)
|
||||||
|
|
||||||
|
args = ValuesArguments([index_value_set])
|
||||||
|
return ValueSet.from_sets(name.infer().execute(args) for name in names)
|
||||||
|
|
||||||
|
def py__iter__(self, contextualized_node=None):
|
||||||
|
iter_slot_names = self.get_function_slot_names('__iter__')
|
||||||
|
if not iter_slot_names:
|
||||||
|
return super().py__iter__(contextualized_node)
|
||||||
|
|
||||||
|
def iterate():
|
||||||
|
for generator in self.execute_function_slots(iter_slot_names):
|
||||||
|
yield from generator.py__next__(contextualized_node)
|
||||||
|
return iterate()
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<%s of %s>" % (self.__class__.__name__, self.class_value)
|
return "<%s of %s>" % (self.__class__.__name__, self.class_value)
|
||||||
|
|
||||||
@@ -237,27 +264,6 @@ class _BaseTreeInstance(AbstractInstanceValue):
|
|||||||
or self.get_function_slot_names('__getattribute__'))
|
or self.get_function_slot_names('__getattribute__'))
|
||||||
return self.execute_function_slots(names, name)
|
return self.execute_function_slots(names, name)
|
||||||
|
|
||||||
def py__getitem__(self, index_value_set, contextualized_node):
|
|
||||||
names = self.get_function_slot_names('__getitem__')
|
|
||||||
if not names:
|
|
||||||
return super().py__getitem__(
|
|
||||||
index_value_set,
|
|
||||||
contextualized_node,
|
|
||||||
)
|
|
||||||
|
|
||||||
args = ValuesArguments([index_value_set])
|
|
||||||
return ValueSet.from_sets(name.infer().execute(args) for name in names)
|
|
||||||
|
|
||||||
def py__iter__(self, contextualized_node=None):
|
|
||||||
iter_slot_names = self.get_function_slot_names('__iter__')
|
|
||||||
if not iter_slot_names:
|
|
||||||
return super().py__iter__(contextualized_node)
|
|
||||||
|
|
||||||
def iterate():
|
|
||||||
for generator in self.execute_function_slots(iter_slot_names):
|
|
||||||
yield from generator.py__next__(contextualized_node)
|
|
||||||
return iterate()
|
|
||||||
|
|
||||||
def py__next__(self, contextualized_node=None):
|
def py__next__(self, contextualized_node=None):
|
||||||
name = u'__next__'
|
name = u'__next__'
|
||||||
next_slot_names = self.get_function_slot_names(name)
|
next_slot_names = self.get_function_slot_names(name)
|
||||||
@@ -295,16 +301,6 @@ class _BaseTreeInstance(AbstractInstanceValue):
|
|||||||
else:
|
else:
|
||||||
return ValueSet([self])
|
return ValueSet([self])
|
||||||
|
|
||||||
def get_function_slot_names(self, name):
|
|
||||||
# Python classes don't look at the dictionary of the instance when
|
|
||||||
# looking up `__call__`. This is something that has to do with Python's
|
|
||||||
# internal slot system (note: not __slots__, but C slots).
|
|
||||||
for filter in self.get_filters(include_self_names=False):
|
|
||||||
names = filter.get(name)
|
|
||||||
if names:
|
|
||||||
return names
|
|
||||||
return []
|
|
||||||
|
|
||||||
|
|
||||||
class TreeInstance(_BaseTreeInstance):
|
class TreeInstance(_BaseTreeInstance):
|
||||||
def __init__(self, inference_state, parent_context, class_value, arguments):
|
def __init__(self, inference_state, parent_context, class_value, arguments):
|
||||||
|
|||||||
@@ -342,6 +342,8 @@ class SequenceLiteralValue(Sequence):
|
|||||||
else:
|
else:
|
||||||
with reraise_getitem_errors(TypeError, KeyError, IndexError):
|
with reraise_getitem_errors(TypeError, KeyError, IndexError):
|
||||||
node = self.get_tree_entries()[index]
|
node = self.get_tree_entries()[index]
|
||||||
|
if node == ':' or node.type == 'subscript':
|
||||||
|
return NO_VALUES
|
||||||
return self._defining_context.infer_node(node)
|
return self._defining_context.infer_node(node)
|
||||||
|
|
||||||
def py__iter__(self, contextualized_node=None):
|
def py__iter__(self, contextualized_node=None):
|
||||||
@@ -407,16 +409,6 @@ class SequenceLiteralValue(Sequence):
|
|||||||
else:
|
else:
|
||||||
return [array_node]
|
return [array_node]
|
||||||
|
|
||||||
def exact_key_items(self):
|
|
||||||
"""
|
|
||||||
Returns a generator of tuples like dict.items(), where the key is
|
|
||||||
resolved (as a string) and the values are still lazy values.
|
|
||||||
"""
|
|
||||||
for key_node, value in self.get_tree_entries():
|
|
||||||
for key in self._defining_context.infer_node(key_node):
|
|
||||||
if is_string(key):
|
|
||||||
yield key.get_safe_value(), LazyTreeValue(self._defining_context, value)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<%s of %s>" % (self.__class__.__name__, self.atom)
|
return "<%s of %s>" % (self.__class__.__name__, self.atom)
|
||||||
|
|
||||||
@@ -472,6 +464,16 @@ class DictLiteralValue(_DictMixin, SequenceLiteralValue, _DictKeyMixin):
|
|||||||
|
|
||||||
return ValueSet([FakeList(self.inference_state, lazy_values)])
|
return ValueSet([FakeList(self.inference_state, lazy_values)])
|
||||||
|
|
||||||
|
def exact_key_items(self):
|
||||||
|
"""
|
||||||
|
Returns a generator of tuples like dict.items(), where the key is
|
||||||
|
resolved (as a string) and the values are still lazy values.
|
||||||
|
"""
|
||||||
|
for key_node, value in self.get_tree_entries():
|
||||||
|
for key in self._defining_context.infer_node(key_node):
|
||||||
|
if is_string(key):
|
||||||
|
yield key.get_safe_value(), LazyTreeValue(self._defining_context, value)
|
||||||
|
|
||||||
def _dict_values(self):
|
def _dict_values(self):
|
||||||
return ValueSet.from_sets(
|
return ValueSet.from_sets(
|
||||||
self._defining_context.infer_node(v)
|
self._defining_context.infer_node(v)
|
||||||
|
|||||||
@@ -75,17 +75,19 @@ class ClassName(TreeNameDefinition):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def api_type(self):
|
def api_type(self):
|
||||||
if self.tree_name is not None:
|
type_ = super().api_type
|
||||||
|
if type_ == 'function':
|
||||||
definition = self.tree_name.get_definition()
|
definition = self.tree_name.get_definition()
|
||||||
if definition.type == 'funcdef':
|
if definition is None:
|
||||||
if function_is_property(definition):
|
return type_
|
||||||
# This essentially checks if there is an @property before
|
if function_is_property(definition):
|
||||||
# the function. @property could be something different, but
|
# This essentially checks if there is an @property before
|
||||||
# any programmer that redefines property as something that
|
# the function. @property could be something different, but
|
||||||
# is not really a property anymore, should be shot. (i.e.
|
# any programmer that redefines property as something that
|
||||||
# this is a heuristic).
|
# is not really a property anymore, should be shot. (i.e.
|
||||||
return 'property'
|
# this is a heuristic).
|
||||||
return super().api_type
|
return 'property'
|
||||||
|
return type_
|
||||||
|
|
||||||
|
|
||||||
class ClassFilter(ParserTreeFilter):
|
class ClassFilter(ParserTreeFilter):
|
||||||
@@ -114,25 +116,10 @@ class ClassFilter(ParserTreeFilter):
|
|||||||
while node is not None:
|
while node is not None:
|
||||||
if node == self._parser_scope or node == self.parent_context:
|
if node == self._parser_scope or node == self.parent_context:
|
||||||
return True
|
return True
|
||||||
node = get_cached_parent_scope(self._used_names, node)
|
node = get_cached_parent_scope(self._parso_cache_node, node)
|
||||||
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()
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ class ModuleMixin(SubModuleDictMixin):
|
|||||||
parent_context=self.as_context(),
|
parent_context=self.as_context(),
|
||||||
origin_scope=origin_scope
|
origin_scope=origin_scope
|
||||||
),
|
),
|
||||||
GlobalNameFilter(self.as_context(), self.tree_node),
|
GlobalNameFilter(self.as_context()),
|
||||||
)
|
)
|
||||||
yield DictFilter(self.sub_modules_dict())
|
yield DictFilter(self.sub_modules_dict())
|
||||||
yield DictFilter(self._module_attributes_dict())
|
yield DictFilter(self._module_attributes_dict())
|
||||||
@@ -148,7 +148,7 @@ class ModuleValue(ModuleMixin, TreeValue):
|
|||||||
if file_io is None:
|
if file_io is None:
|
||||||
self._path: Optional[Path] = None
|
self._path: Optional[Path] = None
|
||||||
else:
|
else:
|
||||||
self._path = Path(file_io.path)
|
self._path = file_io.path
|
||||||
self.string_names = string_names # Optional[Tuple[str, ...]]
|
self.string_names = string_names # Optional[Tuple[str, ...]]
|
||||||
self.code_lines = code_lines
|
self.code_lines = code_lines
|
||||||
self._is_package = is_package
|
self._is_package = is_package
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from jedi.inference.cache import inference_state_method_cache
|
from jedi.inference.cache import inference_state_method_cache
|
||||||
from jedi.inference.filters import DictFilter
|
from jedi.inference.filters import DictFilter
|
||||||
from jedi.inference.names import ValueNameMixin, AbstractNameDefinition
|
from jedi.inference.names import ValueNameMixin, AbstractNameDefinition
|
||||||
@@ -20,10 +23,7 @@ class ImplicitNamespaceValue(Value, SubModuleDictMixin):
|
|||||||
"""
|
"""
|
||||||
Provides support for implicit namespace packages
|
Provides support for implicit namespace packages
|
||||||
"""
|
"""
|
||||||
# Is a module like every other module, because if you import an empty
|
api_type = 'namespace'
|
||||||
# folder foobar it will be available as an object:
|
|
||||||
# <module 'foobar' (namespace)>.
|
|
||||||
api_type = 'module'
|
|
||||||
parent_context = None
|
parent_context = None
|
||||||
|
|
||||||
def __init__(self, inference_state, string_names, paths):
|
def __init__(self, inference_state, string_names, paths):
|
||||||
@@ -44,7 +44,7 @@ class ImplicitNamespaceValue(Value, SubModuleDictMixin):
|
|||||||
string_name = self.py__package__()[-1]
|
string_name = self.py__package__()[-1]
|
||||||
return ImplicitNSName(self, string_name)
|
return ImplicitNSName(self, string_name)
|
||||||
|
|
||||||
def py__file__(self):
|
def py__file__(self) -> Optional[Path]:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def py__package__(self):
|
def py__package__(self):
|
||||||
|
|||||||
@@ -216,11 +216,14 @@ def is_scope(node):
|
|||||||
def _get_parent_scope_cache(func):
|
def _get_parent_scope_cache(func):
|
||||||
cache = WeakKeyDictionary()
|
cache = WeakKeyDictionary()
|
||||||
|
|
||||||
def wrapper(used_names, node, include_flows=False):
|
def wrapper(parso_cache_node, node, include_flows=False):
|
||||||
|
if parso_cache_node is None:
|
||||||
|
return func(node, include_flows)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for_module = cache[used_names]
|
for_module = cache[parso_cache_node]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
for_module = cache[used_names] = {}
|
for_module = cache[parso_cache_node] = {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return for_module[node]
|
return for_module[node]
|
||||||
@@ -270,7 +273,18 @@ def get_cached_code_lines(grammar, path):
|
|||||||
Basically access the cached code lines in parso. This is not the nicest way
|
Basically access the cached code lines in parso. This is not the nicest way
|
||||||
to do this, but we avoid splitting all the lines again.
|
to do this, but we avoid splitting all the lines again.
|
||||||
"""
|
"""
|
||||||
return parser_cache[grammar._hashed][path].lines
|
return get_parso_cache_node(grammar, path).lines
|
||||||
|
|
||||||
|
|
||||||
|
def get_parso_cache_node(grammar, path):
|
||||||
|
"""
|
||||||
|
This is of course not public. But as long as I control parso, this
|
||||||
|
shouldn't be a problem. ~ Dave
|
||||||
|
|
||||||
|
The reason for this is mostly caching. This is obviously also a sign of a
|
||||||
|
broken caching architecture.
|
||||||
|
"""
|
||||||
|
return parser_cache[grammar._hashed][path]
|
||||||
|
|
||||||
|
|
||||||
def cut_value_at_position(leaf, position):
|
def cut_value_at_position(leaf, position):
|
||||||
@@ -306,7 +320,7 @@ def expr_is_dotted(node):
|
|||||||
return node.type == 'name'
|
return node.type == 'name'
|
||||||
|
|
||||||
|
|
||||||
def _function_is_x_method(method_name):
|
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
|
||||||
@@ -316,12 +330,16 @@ def _function_is_x_method(method_name):
|
|||||||
"""
|
"""
|
||||||
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() == method_name:
|
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')
|
function_is_property = _function_is_x_method(
|
||||||
|
lambda m: m == "property"
|
||||||
|
or m == "cached_property"
|
||||||
|
or (m.endswith(".setter"))
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
|
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
|
||||||
|
|
||||||
_PYTEST_FIXTURE_MODULES = [
|
_PYTEST_FIXTURE_MODULES = [
|
||||||
('_pytest', 'monkeypatch'),
|
('_pytest', 'monkeypatch'),
|
||||||
@@ -30,7 +33,15 @@ def execute(callback):
|
|||||||
def infer_anonymous_param(func):
|
def infer_anonymous_param(func):
|
||||||
def get_returns(value):
|
def get_returns(value):
|
||||||
if value.tree_node.annotation is not None:
|
if value.tree_node.annotation is not None:
|
||||||
return value.execute_with_values()
|
result = value.execute_with_values()
|
||||||
|
if any(v.name.get_qualified_names(include_module_names=True)
|
||||||
|
== ('typing', 'Generator')
|
||||||
|
for v in result):
|
||||||
|
return ValueSet.from_sets(
|
||||||
|
v.py__getattribute__('__next__').execute_annotation()
|
||||||
|
for v in result
|
||||||
|
)
|
||||||
|
return result
|
||||||
|
|
||||||
# In pytest we need to differentiate between generators and normal
|
# In pytest we need to differentiate between generators and normal
|
||||||
# returns.
|
# returns.
|
||||||
@@ -42,6 +53,9 @@ def infer_anonymous_param(func):
|
|||||||
return function_context.get_return_values()
|
return function_context.get_return_values()
|
||||||
|
|
||||||
def wrapper(param_name):
|
def wrapper(param_name):
|
||||||
|
# parameters with an annotation do not need special handling
|
||||||
|
if param_name.annotation_node:
|
||||||
|
return func(param_name)
|
||||||
is_pytest_param, param_name_is_function_name = \
|
is_pytest_param, param_name_is_function_name = \
|
||||||
_is_a_pytest_param_and_inherited(param_name)
|
_is_a_pytest_param_and_inherited(param_name)
|
||||||
if is_pytest_param:
|
if is_pytest_param:
|
||||||
@@ -119,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:
|
||||||
@@ -128,37 +172,97 @@ def _iter_pytest_modules(module_context, skip_own_module=False):
|
|||||||
if file_io is not None:
|
if file_io is not None:
|
||||||
folder = file_io.get_parent_folder()
|
folder = file_io.get_parent_folder()
|
||||||
sys_path = module_context.inference_state.get_sys_path()
|
sys_path = module_context.inference_state.get_sys_path()
|
||||||
|
|
||||||
|
# prevent an infinite loop when reaching the root of the current drive
|
||||||
|
last_folder = None
|
||||||
|
|
||||||
while any(folder.path.startswith(p) for p in sys_path):
|
while any(folder.path.startswith(p) for p in sys_path):
|
||||||
file_io = folder.get_file_io('conftest.py')
|
file_io = folder.get_file_io('conftest.py')
|
||||||
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()
|
||||||
|
|
||||||
for names in _PYTEST_FIXTURE_MODULES:
|
# prevent an infinite for loop if the same parent folder is return twice
|
||||||
|
if last_folder is not None and folder.path == last_folder.path:
|
||||||
|
break
|
||||||
|
last_folder = folder # keep track of the last found parent name
|
||||||
|
|
||||||
|
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
|
||||||
if funcdef.type == 'funcdef':
|
if name.parent.type == "import_from":
|
||||||
# Class fixtures are not supported
|
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):
|
||||||
for decorator in decorated.children:
|
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]
|
||||||
|
if decorators.type == 'decorators':
|
||||||
|
decorators = decorators.children
|
||||||
|
else:
|
||||||
|
decorators = [decorators]
|
||||||
|
for decorator in decorators:
|
||||||
dotted_name = decorator.children[1]
|
dotted_name = decorator.children[1]
|
||||||
# A heuristic, this makes it faster.
|
# A heuristic, this makes it faster.
|
||||||
if 'fixture' in dotted_name.get_code():
|
if 'fixture' in dotted_name.get_code():
|
||||||
for value in self.parent_context.infer_node(dotted_name):
|
if dotted_name.type == 'atom_expr':
|
||||||
|
# Since Python3.9 a decorator does not have dotted names
|
||||||
|
# anymore.
|
||||||
|
last_trailer = dotted_name.children[-1]
|
||||||
|
last_leaf = last_trailer.get_last_leaf()
|
||||||
|
if last_leaf == ')':
|
||||||
|
values = infer_call_of_leaf(
|
||||||
|
context, last_leaf, cut_own_trailer=True
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
values = context.infer_node(dotted_name)
|
||||||
|
else:
|
||||||
|
values = context.infer_node(dotted_name)
|
||||||
|
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'):
|
||||||
return True
|
return True
|
||||||
|
|||||||
@@ -803,6 +803,17 @@ _implemented = {
|
|||||||
# 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.
|
||||||
'dataclass': _dataclass
|
'dataclass': _dataclass
|
||||||
},
|
},
|
||||||
|
# attrs exposes declaration interface roughly compatible with dataclasses
|
||||||
|
# via attrs.define, attrs.frozen and attrs.mutable
|
||||||
|
# https://www.attrs.org/en/stable/names.html
|
||||||
|
'attr': {
|
||||||
|
'define': _dataclass,
|
||||||
|
'frozen': _dataclass,
|
||||||
|
},
|
||||||
|
'attrs': {
|
||||||
|
'define': _dataclass,
|
||||||
|
'frozen': _dataclass,
|
||||||
|
},
|
||||||
'os.path': {
|
'os.path': {
|
||||||
'dirname': _create_string_input_function(os.path.dirname),
|
'dirname': _create_string_input_function(os.path.dirname),
|
||||||
'abspath': _create_string_input_function(os.path.abspath),
|
'abspath': _create_string_input_function(os.path.abspath),
|
||||||
|
|||||||
@@ -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
jedi/third_party/typeshed
vendored
2
jedi/third_party/typeshed
vendored
Submodule jedi/third_party/typeshed updated: d386452478...ae9d4f4b21
@@ -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
|
||||||
|
|||||||
51
setup.py
51
setup.py
@@ -27,24 +27,61 @@ 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.6',
|
||||||
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<6.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',
|
||||||
],
|
],
|
||||||
'qa': [
|
'qa': [
|
||||||
'flake8==3.8.3',
|
# latest version supporting Python 3.6
|
||||||
'mypy==0.782',
|
'flake8==5.0.4',
|
||||||
|
# latest version supporting Python 3.6
|
||||||
|
'mypy==0.971',
|
||||||
|
# Arbitrary pins, latest at the time of pinning
|
||||||
|
'types-setuptools==67.2.0.1',
|
||||||
|
],
|
||||||
|
'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',
|
||||||
@@ -61,6 +98,10 @@ setup(name='jedi',
|
|||||||
'Programming Language :: Python :: 3.7',
|
'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.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',
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ b[int():]
|
|||||||
|
|
||||||
#? list()
|
#? list()
|
||||||
b[:]
|
b[:]
|
||||||
|
#? int()
|
||||||
|
b[:, :-1]
|
||||||
|
|
||||||
#? 3
|
#? 3
|
||||||
b[:]
|
b[:]
|
||||||
@@ -67,6 +69,20 @@ class _StrangeSlice():
|
|||||||
#? slice()
|
#? slice()
|
||||||
_StrangeSlice()[1:2]
|
_StrangeSlice()[1:2]
|
||||||
|
|
||||||
|
for x in b[:]:
|
||||||
|
#? int()
|
||||||
|
x
|
||||||
|
|
||||||
|
for x in b[:, :-1]:
|
||||||
|
#?
|
||||||
|
x
|
||||||
|
|
||||||
|
class Foo:
|
||||||
|
def __getitem__(self, item):
|
||||||
|
return item
|
||||||
|
|
||||||
|
#?
|
||||||
|
Foo()[:, :-1][0]
|
||||||
|
|
||||||
# -----------------
|
# -----------------
|
||||||
# iterable multiplication
|
# iterable multiplication
|
||||||
@@ -214,6 +230,20 @@ f
|
|||||||
#? str()
|
#? str()
|
||||||
g
|
g
|
||||||
|
|
||||||
|
# -----------------
|
||||||
|
# setitem
|
||||||
|
# -----------------
|
||||||
|
|
||||||
|
class F:
|
||||||
|
setitem_x = [1,2]
|
||||||
|
setitem_x[0] = 3
|
||||||
|
|
||||||
|
#? ['setitem_x']
|
||||||
|
F().setitem_x
|
||||||
|
#? list()
|
||||||
|
F().setitem_x
|
||||||
|
|
||||||
|
|
||||||
# -----------------
|
# -----------------
|
||||||
# dicts
|
# dicts
|
||||||
# -----------------
|
# -----------------
|
||||||
|
|||||||
@@ -26,11 +26,6 @@ async def y():
|
|||||||
x().__await__().__next
|
x().__await__().__next
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
async def x2():
|
|
||||||
async with open('asdf') as f:
|
|
||||||
#? ['readlines']
|
|
||||||
f.readlines
|
|
||||||
|
|
||||||
class A():
|
class A():
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def b(c=1, d=2):
|
async def b(c=1, d=2):
|
||||||
@@ -52,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)
|
||||||
@@ -105,3 +98,22 @@ async def f():
|
|||||||
f = await C().async_for_classmethod()
|
f = await C().async_for_classmethod()
|
||||||
#? C()
|
#? C()
|
||||||
f
|
f
|
||||||
|
|
||||||
|
|
||||||
|
class AsyncCtxMgr:
|
||||||
|
def some_method():
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def __aenter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def __aexit__(self, *args):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
async def asyncctxmgr():
|
||||||
|
async with AsyncCtxMgr() as acm:
|
||||||
|
#? AsyncCtxMgr()
|
||||||
|
acm
|
||||||
|
#? ['some_method']
|
||||||
|
acm.som
|
||||||
|
|||||||
@@ -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):
|
||||||
|
|||||||
@@ -284,6 +284,13 @@ def doctest_with_space():
|
|||||||
import_issu
|
import_issu
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def doctest_issue_github_1748():
|
||||||
|
"""From GitHub #1748
|
||||||
|
#? 10 []
|
||||||
|
This. Al
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def docstring_rst_identifiers():
|
def docstring_rst_identifiers():
|
||||||
"""
|
"""
|
||||||
|
|||||||
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
|
||||||
|
|
||||||
|
|||||||
@@ -309,3 +309,8 @@ def annotation2() -> Iterator[float]:
|
|||||||
next(annotation1())
|
next(annotation1())
|
||||||
#? float()
|
#? float()
|
||||||
next(annotation2())
|
next(annotation2())
|
||||||
|
|
||||||
|
|
||||||
|
# annotations should override generator inference
|
||||||
|
#? float()
|
||||||
|
annotation1()
|
||||||
|
|||||||
@@ -202,6 +202,10 @@ from keyword import not_existing1, not_existing2
|
|||||||
from tokenize import io
|
from tokenize import io
|
||||||
tokenize.generate_tokens
|
tokenize.generate_tokens
|
||||||
|
|
||||||
|
import socket
|
||||||
|
#? 14 ['SocketIO']
|
||||||
|
socket.SocketIO
|
||||||
|
|
||||||
# -----------------
|
# -----------------
|
||||||
# builtins
|
# builtins
|
||||||
# -----------------
|
# -----------------
|
||||||
|
|||||||
@@ -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]
|
||||||
|
|||||||
1
test/completion/namespace1/pkg1/pkg2/mod1.py
Normal file
1
test/completion/namespace1/pkg1/pkg2/mod1.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
mod1_name = 'mod1'
|
||||||
1
test/completion/namespace2/pkg1/pkg2/mod2.py
Normal file
1
test/completion/namespace2/pkg1/pkg2/mod2.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
mod2_name = 'mod2'
|
||||||
18
test/completion/ns_path.py
Normal file
18
test/completion/ns_path.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import sys
|
||||||
|
import os
|
||||||
|
from os.path import dirname
|
||||||
|
|
||||||
|
sys.path.insert(0, os.path.join(dirname(__file__), 'namespace2'))
|
||||||
|
sys.path.insert(0, os.path.join(dirname(__file__), 'namespace1'))
|
||||||
|
|
||||||
|
#? ['mod1']
|
||||||
|
import pkg1.pkg2.mod1
|
||||||
|
|
||||||
|
#? ['mod2']
|
||||||
|
import pkg1.pkg2.mod2
|
||||||
|
|
||||||
|
#? ['mod1_name']
|
||||||
|
pkg1.pkg2.mod1.mod1_name
|
||||||
|
|
||||||
|
#? ['mod2_name']
|
||||||
|
pkg1.pkg2.mod2.mod2_name
|
||||||
@@ -23,11 +23,9 @@ def builtin_test():
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
# classes is a local module that has an __init__.py and can therefore not be
|
# classes is a local module that has an __init__.py and can therefore not be
|
||||||
# found. test can be found.
|
# found.
|
||||||
#? []
|
#? []
|
||||||
import classes
|
import classes
|
||||||
#? ['test']
|
|
||||||
import test
|
|
||||||
|
|
||||||
#? ['timedelta']
|
#? ['timedelta']
|
||||||
from datetime import timedel
|
from datetime import timedel
|
||||||
@@ -78,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):
|
||||||
@@ -58,6 +58,16 @@ def typed_bound_generic_passthrough(x: TList) -> TList:
|
|||||||
|
|
||||||
return x
|
return x
|
||||||
|
|
||||||
|
# Forward references are more likely with custom types, however this aims to
|
||||||
|
# test just the handling of the quoted type rather than any other part of the
|
||||||
|
# machinery.
|
||||||
|
def typed_quoted_return_generic_passthrough(x: T) -> 'List[T]':
|
||||||
|
return [x]
|
||||||
|
|
||||||
|
def typed_quoted_input_generic_passthrough(x: 'Tuple[T]') -> T:
|
||||||
|
x
|
||||||
|
return x[0]
|
||||||
|
|
||||||
|
|
||||||
for a in untyped_passthrough(untyped_list_str):
|
for a in untyped_passthrough(untyped_list_str):
|
||||||
#? str()
|
#? str()
|
||||||
@@ -146,6 +156,23 @@ for q in typed_bound_generic_passthrough(typed_list_str):
|
|||||||
q
|
q
|
||||||
|
|
||||||
|
|
||||||
|
for r in typed_quoted_return_generic_passthrough("something"):
|
||||||
|
#? str()
|
||||||
|
r
|
||||||
|
|
||||||
|
for s in typed_quoted_return_generic_passthrough(42):
|
||||||
|
#? int()
|
||||||
|
s
|
||||||
|
|
||||||
|
|
||||||
|
#? str()
|
||||||
|
typed_quoted_input_generic_passthrough(("something",))
|
||||||
|
|
||||||
|
#? int()
|
||||||
|
typed_quoted_input_generic_passthrough((42,))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class CustomList(List):
|
class CustomList(List):
|
||||||
def get_first(self):
|
def get_first(self):
|
||||||
return self[0]
|
return self[0]
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -54,7 +54,7 @@ a
|
|||||||
|
|
||||||
#? int()
|
#? int()
|
||||||
(3 ** 3)
|
(3 ** 3)
|
||||||
#? int() str()
|
#? int()
|
||||||
(3 ** 'a')
|
(3 ** 'a')
|
||||||
#? int()
|
#? int()
|
||||||
(3 + 'a')
|
(3 + 'a')
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from typing import Generator
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from pytest import fixture
|
from pytest import fixture
|
||||||
|
|
||||||
@@ -64,6 +66,11 @@ def lala(my_fixture):
|
|||||||
def lala(my_fixture):
|
def lala(my_fixture):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# overriding types of a fixture should be possible
|
||||||
|
def test_x(my_yield_fixture: str):
|
||||||
|
#? str()
|
||||||
|
my_yield_fixture
|
||||||
|
|
||||||
# -----------------
|
# -----------------
|
||||||
# completion
|
# completion
|
||||||
# -----------------
|
# -----------------
|
||||||
@@ -89,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
|
||||||
@@ -132,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
|
||||||
@@ -164,3 +171,40 @@ def test_inheritance_fixture(inheritance_fixture, caplog):
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def caplog(caplog):
|
def caplog(caplog):
|
||||||
yield caplog
|
yield caplog
|
||||||
|
|
||||||
|
# -----------------
|
||||||
|
# Generator with annotation
|
||||||
|
# -----------------
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def with_annot() -> Generator[float, None, None]:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_with_annot(inheritance_fixture, with_annot):
|
||||||
|
#? float()
|
||||||
|
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
|
||||||
|
|
||||||
|
|||||||
@@ -310,6 +310,13 @@ z = 3
|
|||||||
#< 10 (0,1), (0,10)
|
#< 10 (0,1), (0,10)
|
||||||
{z:1 for z in something}
|
{z:1 for z in something}
|
||||||
|
|
||||||
|
#< 8 (0,6), (0, 40)
|
||||||
|
[[x + nested_loopv2 for x in bar()] for nested_loopv2 in baz()]
|
||||||
|
|
||||||
|
#< 25 (0,20), (0, 65)
|
||||||
|
(("*" if abs(foo(x, nested_loopv1)) else " " for x in bar()) for nested_loopv1 in baz())
|
||||||
|
|
||||||
|
|
||||||
def whatever_func():
|
def whatever_func():
|
||||||
zzz = 3
|
zzz = 3
|
||||||
if UNDEFINED:
|
if UNDEFINED:
|
||||||
@@ -376,3 +383,12 @@ usage_definition = 1
|
|||||||
if False:
|
if False:
|
||||||
#< 8 (-3, 0), (0, 4), ('import_tree.references', 1, 21), ('import_tree.references', 5, 4)
|
#< 8 (-3, 0), (0, 4), ('import_tree.references', 1, 21), ('import_tree.references', 5, 4)
|
||||||
usage_definition()
|
usage_definition()
|
||||||
|
|
||||||
|
# -----------------
|
||||||
|
# stdlib stuff
|
||||||
|
# -----------------
|
||||||
|
|
||||||
|
import socket
|
||||||
|
#< (1, 21), (0, 7), ('socket', ..., 6), ('stub:socket', ..., 4), ('imports', ..., 7)
|
||||||
|
socket.SocketIO
|
||||||
|
some_socket = socket.SocketIO()
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user