forked from VimPlug/jedi
Compare commits
110 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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
|
||||||
73
.github/workflows/ci.yml
vendored
Normal file
73
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
name: ci
|
||||||
|
on: [push, pull_request]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
tests:
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu-20.04, windows-2019]
|
||||||
|
python-version: ["3.10", "3.9", "3.8", "3.7", "3.6"]
|
||||||
|
environment: ['3.8', '3.10', '3.9', '3.7', '3.6', 'interpreter']
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
|
||||||
|
- uses: actions/setup-python@v2
|
||||||
|
if: ${{ matrix.environment != 'interpreter' }}
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.environment }}
|
||||||
|
|
||||||
|
- uses: actions/setup-python@v2
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.python-version }}
|
||||||
|
|
||||||
|
- 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@v2
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: 'pip install .[qa]'
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: |
|
||||||
|
python -m flake8 jedi setup.py
|
||||||
|
python -m mypy jedi sith.py
|
||||||
|
|
||||||
|
coverage:
|
||||||
|
runs-on: ubuntu-20.04
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
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
|
||||||
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
|
|
||||||
@@ -61,6 +61,7 @@ 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)
|
||||||
|
|
||||||
And a few more "anonymous" contributors.
|
And a few more "anonymous" contributors.
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,13 @@ Changelog
|
|||||||
Unreleased
|
Unreleased
|
||||||
++++++++++
|
++++++++++
|
||||||
|
|
||||||
|
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)
|
||||||
+++++++++++++++++++
|
+++++++++++++++++++
|
||||||
|
|
||||||
|
|||||||
14
README.rst
14
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
|
||||||
|
|||||||
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)
|
||||||
|
|||||||
@@ -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.
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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.18.1'
|
||||||
|
|
||||||
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
|
||||||
|
|||||||
@@ -7,22 +7,6 @@ import sys
|
|||||||
import pickle
|
import pickle
|
||||||
|
|
||||||
|
|
||||||
def cast_path(string):
|
|
||||||
"""
|
|
||||||
Take a bytes or str path and cast it to unicode.
|
|
||||||
|
|
||||||
Apparently it is perfectly fine to pass both byte and unicode objects into
|
|
||||||
the sys.path. This probably means that byte paths are normal at other
|
|
||||||
places as well.
|
|
||||||
|
|
||||||
Since this just really complicates everything and Python 2.7 will be EOL
|
|
||||||
soon anyway, just go with always strings.
|
|
||||||
"""
|
|
||||||
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 pickle.load(file)
|
||||||
|
|||||||
@@ -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(
|
||||||
@@ -393,7 +394,7 @@ 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.
|
||||||
@@ -709,7 +710,7 @@ class Interpreter(Script):
|
|||||||
"""
|
"""
|
||||||
_allow_descriptor_getattr_default = True
|
_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 +723,23 @@ 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_descriptor_getattr = self._allow_descriptor_getattr_default
|
||||||
|
|
||||||
@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)]
|
||||||
@@ -550,6 +554,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 +763,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
|
||||||
@@ -194,7 +195,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 +277,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():
|
||||||
@@ -462,12 +466,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 +631,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(
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import parso
|
|||||||
|
|
||||||
_VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
_VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||||
|
|
||||||
_SUPPORTED_PYTHONS = ['3.9', '3.8', '3.7', '3.6']
|
_SUPPORTED_PYTHONS = ['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)
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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)
|
||||||
@@ -439,5 +446,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')
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -156,8 +156,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")
|
||||||
|
|
||||||
|
|||||||
@@ -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:
|
||||||
|
|||||||
@@ -181,8 +181,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):
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
from jedi.inference.compiled.getattr_static import getattr_static
|
from jedi.inference.compiled.getattr_static import getattr_static
|
||||||
|
|
||||||
@@ -179,9 +181,9 @@ class DirectObjectAccess:
|
|||||||
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,7 +213,22 @@ 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]
|
||||||
|
|
||||||
|
if self.is_instance():
|
||||||
|
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):
|
def py__simple_getitem__(self, index):
|
||||||
if type(self._obj) not in ALLOWED_GETITEM_TYPES:
|
if type(self._obj) not in ALLOWED_GETITEM_TYPES:
|
||||||
@@ -221,8 +238,14 @@ class DirectObjectAccess:
|
|||||||
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,9 +329,9 @@ 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):
|
||||||
# TODO this API is ugly.
|
# TODO this API is ugly.
|
||||||
if unsafe:
|
if not safe:
|
||||||
# Unsafe is mostly used to check for __getattr__/__getattribute__.
|
# Unsafe is mostly used to check for __getattr__/__getattribute__.
|
||||||
# getattr_static works for properties, but the underscore methods
|
# getattr_static works for properties, but the underscore methods
|
||||||
# are just ignored (because it's safer and avoids more code
|
# are just ignored (because it's safer and avoids more code
|
||||||
@@ -361,7 +384,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
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ goals:
|
|||||||
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.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import collections
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import queue
|
import queue
|
||||||
@@ -168,7 +169,7 @@ class CompiledSubprocess:
|
|||||||
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):
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ import inspect
|
|||||||
import importlib
|
import importlib
|
||||||
import warnings
|
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 +15,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 +93,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():
|
||||||
@@ -190,7 +197,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 +219,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 +237,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, \
|
||||||
@@ -167,7 +167,7 @@ class CompiledValue(Value):
|
|||||||
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 +293,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,11 +306,8 @@ 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):
|
||||||
@@ -440,7 +434,7 @@ class CompiledValueFilter(AbstractFilter):
|
|||||||
access_handle = self.compiled_value.access_handle
|
access_handle = self.compiled_value.access_handle
|
||||||
return self._get(
|
return self._get(
|
||||||
name,
|
name,
|
||||||
lambda name, unsafe: access_handle.is_allowed_getattr(name, unsafe),
|
lambda name, safe: 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
|
||||||
)
|
)
|
||||||
@@ -454,7 +448,7 @@ class CompiledValueFilter(AbstractFilter):
|
|||||||
|
|
||||||
has_attribute, is_descriptor = allowed_getattr_callback(
|
has_attribute, is_descriptor = allowed_getattr_callback(
|
||||||
name,
|
name,
|
||||||
unsafe=self._inference_state.allow_descriptor_getattr
|
safe=not self._inference_state.allow_descriptor_getattr
|
||||||
)
|
)
|
||||||
if check_has_attribute and not has_attribute:
|
if check_has_attribute and not has_attribute:
|
||||||
return []
|
return []
|
||||||
@@ -478,7 +472,7 @@ class CompiledValueFilter(AbstractFilter):
|
|||||||
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 +480,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, safe: dir_infos[name],
|
||||||
lambda name: name in dir_infos,
|
lambda name: name in dir_infos,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -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):
|
||||||
|
|||||||
@@ -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:
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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()
|
||||||
|
|
||||||
|
|
||||||
@@ -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):
|
||||||
@@ -395,7 +397,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 +431,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):
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -250,6 +250,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 +274,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
|
||||||
|
|||||||
@@ -738,6 +738,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 []
|
||||||
|
|||||||
@@ -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,17 @@ 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 function_is_property(definition):
|
||||||
if function_is_property(definition):
|
# This essentially checks if there is an @property before
|
||||||
# This essentially checks if there is an @property before
|
# the function. @property could be something different, but
|
||||||
# the function. @property could be something different, but
|
# any programmer that redefines property as something that
|
||||||
# any programmer that redefines property as something that
|
# is not really a property anymore, should be shot. (i.e.
|
||||||
# is not really a property anymore, should be shot. (i.e.
|
# this is a heuristic).
|
||||||
# this is a heuristic).
|
return 'property'
|
||||||
return 'property'
|
return type_
|
||||||
return super().api_type
|
|
||||||
|
|
||||||
|
|
||||||
class ClassFilter(ParserTreeFilter):
|
class ClassFilter(ParserTreeFilter):
|
||||||
@@ -114,7 +114,7 @@ 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):
|
||||||
|
|||||||
@@ -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(*method_names):
|
||||||
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,7 +330,7 @@ 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 dotted_name.get_code() in method_names:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
return wrapper
|
return wrapper
|
||||||
@@ -324,4 +338,4 @@ def _function_is_x_method(method_name):
|
|||||||
|
|
||||||
function_is_staticmethod = _function_is_x_method('staticmethod')
|
function_is_staticmethod = _function_is_x_method('staticmethod')
|
||||||
function_is_classmethod = _function_is_x_method('classmethod')
|
function_is_classmethod = _function_is_x_method('classmethod')
|
||||||
function_is_property = _function_is_x_method('property')
|
function_is_property = _function_is_x_method('property', 'cached_property')
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ from jedi.inference.cache import inference_state_method_cache
|
|||||||
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.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 +31,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 +51,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:
|
||||||
@@ -128,6 +140,10 @@ 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__():
|
||||||
@@ -138,6 +154,11 @@ def _iter_pytest_modules(module_context, skip_own_module=False):
|
|||||||
pass
|
pass
|
||||||
folder = folder.get_parent_folder()
|
folder = folder.get_parent_folder()
|
||||||
|
|
||||||
|
# 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:
|
for names in _PYTEST_FIXTURE_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()
|
||||||
@@ -147,18 +168,35 @@ 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
|
funcdef = name.parent
|
||||||
|
# Class fixtures are not supported
|
||||||
if funcdef.type == 'funcdef':
|
if funcdef.type == 'funcdef':
|
||||||
# Class fixtures are not supported
|
|
||||||
decorated = funcdef.parent
|
decorated = funcdef.parent
|
||||||
if decorated.type == 'decorated' and self._is_fixture(decorated):
|
if decorated.type == 'decorated' and self._is_fixture(decorated):
|
||||||
yield name
|
yield name
|
||||||
|
|
||||||
def _is_fixture(self, decorated):
|
def _is_fixture(self, decorated):
|
||||||
for decorator in decorated.children:
|
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(
|
||||||
|
self.parent_context, last_leaf, cut_own_trailer=True)
|
||||||
|
else:
|
||||||
|
values = self.parent_context.infer_node(dotted_name)
|
||||||
|
else:
|
||||||
|
values = self.parent_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
|
||||||
|
|||||||
2
jedi/third_party/typeshed
vendored
2
jedi/third_party/typeshed
vendored
Submodule jedi/third_party/typeshed updated: d386452478...ae9d4f4b21
3
setup.py
3
setup.py
@@ -35,7 +35,7 @@ setup(name='jedi',
|
|||||||
install_requires=['parso>=0.8.0,<0.9.0'],
|
install_requires=['parso>=0.8.0,<0.9.0'],
|
||||||
extras_require={
|
extras_require={
|
||||||
'testing': [
|
'testing': [
|
||||||
'pytest<6.0.0',
|
'pytest<7.0.0',
|
||||||
# docopt for sith doctests
|
# docopt for sith doctests
|
||||||
'docopt',
|
'docopt',
|
||||||
# coloroma for colored debug output
|
# coloroma for colored debug output
|
||||||
@@ -61,6 +61,7 @@ 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',
|
||||||
'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):
|
||||||
@@ -105,3 +100,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
|
||||||
|
|||||||
@@ -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():
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -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
|
||||||
# -----------------
|
# -----------------
|
||||||
|
|||||||
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
|
||||||
|
|||||||
@@ -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]
|
||||||
|
|||||||
@@ -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
|
||||||
# -----------------
|
# -----------------
|
||||||
@@ -164,3 +171,15 @@ 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
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import subprocess
|
import subprocess
|
||||||
from itertools import count
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@@ -10,9 +9,6 @@ from . import run
|
|||||||
from . import refactor
|
from . import refactor
|
||||||
from jedi import InterpreterEnvironment, get_system_environment
|
from jedi import InterpreterEnvironment, get_system_environment
|
||||||
from jedi.inference.compiled.value import create_from_access_path
|
from jedi.inference.compiled.value import create_from_access_path
|
||||||
from jedi.inference.imports import _load_python_module
|
|
||||||
from jedi.file_io import KnownContentFileIO
|
|
||||||
from jedi.inference.base_value import ValueSet
|
|
||||||
from jedi.api.interpreter import MixedModuleContext
|
from jedi.api.interpreter import MixedModuleContext
|
||||||
|
|
||||||
# For interpreter tests sometimes the path of this directory is in the sys
|
# For interpreter tests sometimes the path of this directory is in the sys
|
||||||
@@ -163,19 +159,6 @@ def create_compiled_object(inference_state):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def module_injector():
|
|
||||||
counter = count()
|
|
||||||
|
|
||||||
def module_injector(inference_state, names, code):
|
|
||||||
assert isinstance(names, tuple)
|
|
||||||
file_io = KnownContentFileIO('/foo/bar/module-injector-%s.py' % next(counter), code)
|
|
||||||
v = _load_python_module(inference_state, file_io, names)
|
|
||||||
inference_state.module_cache.add(names, ValueSet([v]))
|
|
||||||
|
|
||||||
return module_injector
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(params=[False, True])
|
@pytest.fixture(params=[False, True])
|
||||||
def class_findable(monkeypatch, request):
|
def class_findable(monkeypatch, request):
|
||||||
if not request.param:
|
if not request.param:
|
||||||
|
|||||||
@@ -82,9 +82,9 @@ def _collect_file_tests(code, path, lines_to_execute):
|
|||||||
|
|
||||||
yield RefactoringCase(name, first, line_nr, index, path, kwargs, type_, second)
|
yield RefactoringCase(name, first, line_nr, index, path, kwargs, type_, second)
|
||||||
if match is None:
|
if match is None:
|
||||||
raise Exception("Didn't match any test")
|
raise Exception(f"Didn't match any test for {path}, {code!r}")
|
||||||
if match.end() != len(code):
|
if match.end() != len(code):
|
||||||
raise Exception("Didn't match until the end of the file in %s" % path)
|
raise Exception(f"Didn't match until the end of the file in {path}")
|
||||||
|
|
||||||
|
|
||||||
def collect_dir_tests(base_dir, test_files):
|
def collect_dir_tests(base_dir, test_files):
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ from import_tree import inline_mod
|
|||||||
#? 11 error
|
#? 11 error
|
||||||
test(inline_mod)
|
test(inline_mod)
|
||||||
# ++++++++++++++++++++++++++++++++++++++++++++++++++
|
# ++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||||
Cannot inline imports or modules
|
Cannot inline imports, modules or namespaces
|
||||||
# -------------------------------------------------- module-works
|
# -------------------------------------------------- module-works
|
||||||
from import_tree import inline_mod
|
from import_tree import inline_mod
|
||||||
#? 22
|
#? 22
|
||||||
|
|||||||
28
test/run.py
28
test/run.py
@@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python3
|
||||||
"""
|
"""
|
||||||
|jedi| is mostly being tested by what I would call "integration tests". These
|
|jedi| is mostly being tested by what I would call "integration tests". These
|
||||||
tests are testing type inference with the public API. This makes a
|
tests are testing type inference with the public API. This makes a
|
||||||
@@ -104,9 +104,14 @@ import os
|
|||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import operator
|
import operator
|
||||||
from ast import literal_eval
|
if sys.version_info < (3, 8):
|
||||||
|
literal_eval = eval
|
||||||
|
else:
|
||||||
|
from ast import literal_eval
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
|
from unittest.mock import ANY
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import parso
|
import parso
|
||||||
from _pytest.outcomes import Skipped
|
from _pytest.outcomes import Skipped
|
||||||
@@ -121,6 +126,7 @@ from jedi.api.environment import get_default_environment, get_system_environment
|
|||||||
from jedi.inference.gradual.conversion import convert_values
|
from jedi.inference.gradual.conversion import convert_values
|
||||||
from jedi.inference.analysis import Warning
|
from jedi.inference.analysis import Warning
|
||||||
|
|
||||||
|
test_dir = Path(__file__).absolute().parent
|
||||||
|
|
||||||
TEST_COMPLETIONS = 0
|
TEST_COMPLETIONS = 0
|
||||||
TEST_INFERENCE = 1
|
TEST_INFERENCE = 1
|
||||||
@@ -172,6 +178,7 @@ class IntegrationTestCase(BaseTestCase):
|
|||||||
self.start = start
|
self.start = start
|
||||||
self.line = line
|
self.line = line
|
||||||
self.path = path
|
self.path = path
|
||||||
|
self._project = jedi.Project(test_dir)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def module_name(self):
|
def module_name(self):
|
||||||
@@ -187,7 +194,12 @@ class IntegrationTestCase(BaseTestCase):
|
|||||||
self.line_nr_test, self.line.rstrip())
|
self.line_nr_test, self.line.rstrip())
|
||||||
|
|
||||||
def script(self, environment):
|
def script(self, environment):
|
||||||
return jedi.Script(self.source, path=self.path, environment=environment)
|
return jedi.Script(
|
||||||
|
self.source,
|
||||||
|
path=self.path,
|
||||||
|
environment=environment,
|
||||||
|
project=self._project
|
||||||
|
)
|
||||||
|
|
||||||
def run(self, compare_cb, environment=None):
|
def run(self, compare_cb, environment=None):
|
||||||
testers = {
|
testers = {
|
||||||
@@ -209,6 +221,9 @@ class IntegrationTestCase(BaseTestCase):
|
|||||||
# import cProfile; cProfile.run('...')
|
# import cProfile; cProfile.run('...')
|
||||||
|
|
||||||
comp_str = {c.name for c in completions}
|
comp_str = {c.name for c in completions}
|
||||||
|
for r in completions:
|
||||||
|
# Test if this access raises an error
|
||||||
|
assert isinstance(r.type, str)
|
||||||
return compare_cb(self, comp_str, set(literal_eval(self.correct)))
|
return compare_cb(self, comp_str, set(literal_eval(self.correct)))
|
||||||
|
|
||||||
def run_inference(self, compare_cb, environment):
|
def run_inference(self, compare_cb, environment):
|
||||||
@@ -244,6 +259,9 @@ class IntegrationTestCase(BaseTestCase):
|
|||||||
should = definition(self.correct, self.start, script.path)
|
should = definition(self.correct, self.start, script.path)
|
||||||
result = script.infer(self.line_nr, self.column)
|
result = script.infer(self.line_nr, self.column)
|
||||||
is_str = set(comparison(r) for r in result)
|
is_str = set(comparison(r) for r in result)
|
||||||
|
for r in result:
|
||||||
|
# Test if this access raises an error
|
||||||
|
assert isinstance(r.type, str)
|
||||||
return compare_cb(self, is_str, should)
|
return compare_cb(self, is_str, should)
|
||||||
|
|
||||||
def run_goto(self, compare_cb, environment):
|
def run_goto(self, compare_cb, environment):
|
||||||
@@ -256,7 +274,7 @@ class IntegrationTestCase(BaseTestCase):
|
|||||||
self.correct = self.correct.strip()
|
self.correct = self.correct.strip()
|
||||||
compare = sorted(
|
compare = sorted(
|
||||||
(('stub:' if r.is_stub() else '')
|
(('stub:' if r.is_stub() else '')
|
||||||
+ re.sub(r'^test\.completion\.', '', r.module_name),
|
+ re.sub(r'^completion\.', '', r.module_name),
|
||||||
r.line,
|
r.line,
|
||||||
r.column)
|
r.column)
|
||||||
for r in result
|
for r in result
|
||||||
@@ -269,6 +287,8 @@ class IntegrationTestCase(BaseTestCase):
|
|||||||
for pos_tup in positions:
|
for pos_tup in positions:
|
||||||
if type(pos_tup[0]) == str:
|
if type(pos_tup[0]) == str:
|
||||||
# this means that there is a module specified
|
# this means that there is a module specified
|
||||||
|
if pos_tup[1] == ...:
|
||||||
|
pos_tup = pos_tup[0], ANY, pos_tup[2]
|
||||||
wanted.append(pos_tup)
|
wanted.append(pos_tup)
|
||||||
else:
|
else:
|
||||||
line = pos_tup[0]
|
line = pos_tup[0]
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
with open() as fin:
|
|
||||||
fin.read()
|
|
||||||
@@ -169,10 +169,11 @@ def test_reference_description(Script):
|
|||||||
|
|
||||||
def test_get_line_code(Script):
|
def test_get_line_code(Script):
|
||||||
def get_line_code(source, line=None, **kwargs):
|
def get_line_code(source, line=None, **kwargs):
|
||||||
return Script(source).complete(line=line)[0].get_line_code(**kwargs)
|
# On Windows replace \r
|
||||||
|
return Script(source).complete(line=line)[0].get_line_code(**kwargs).replace('\r', '')
|
||||||
|
|
||||||
# On builtin
|
# On builtin
|
||||||
assert get_line_code('abs') == 'def abs(__n: SupportsAbs[_T]) -> _T: ...\n'
|
assert get_line_code('abs') == 'def abs(__x: SupportsAbs[_T]) -> _T: ...\n'
|
||||||
|
|
||||||
# On custom code
|
# On custom code
|
||||||
first_line = 'def foo():\n'
|
first_line = 'def foo():\n'
|
||||||
|
|||||||
@@ -39,12 +39,11 @@ class TestSignatures(TestCase):
|
|||||||
run = self._run_simple
|
run = self._run_simple
|
||||||
|
|
||||||
# simple
|
# simple
|
||||||
s1 = "sorted(a, bool("
|
s1 = "tuple(a, bool("
|
||||||
run(s1, 'sorted', 0, 7)
|
run(s1, 'tuple', 0, 6)
|
||||||
run(s1, 'sorted', 1, 9)
|
run(s1, 'tuple', None, 8)
|
||||||
run(s1, 'sorted', 1, 10)
|
run(s1, 'tuple', None, 9)
|
||||||
run(s1, 'sorted', None, 11)
|
run(s1, 'bool', 0, 14)
|
||||||
run(s1, 'bool', 0, 15)
|
|
||||||
|
|
||||||
s2 = "abs(), "
|
s2 = "abs(), "
|
||||||
run(s2, 'abs', 0, 4)
|
run(s2, 'abs', 0, 4)
|
||||||
@@ -65,9 +64,9 @@ class TestSignatures(TestCase):
|
|||||||
run(s4, 'abs', 0, 10)
|
run(s4, 'abs', 0, 10)
|
||||||
run(s4, 'abs', None, 11)
|
run(s4, 'abs', None, 11)
|
||||||
|
|
||||||
s5 = "sorted(1,\nif 2:\n def a():"
|
s5 = "tuple(1,\nif 2:\n def a():"
|
||||||
run(s5, 'sorted', 0, 7)
|
run(s5, 'tuple', 0, 6)
|
||||||
run(s5, 'sorted', 1, 9)
|
run(s5, 'tuple', None, 8)
|
||||||
|
|
||||||
s6 = "bool().__eq__("
|
s6 = "bool().__eq__("
|
||||||
run(s6, '__eq__', 0)
|
run(s6, '__eq__', 0)
|
||||||
@@ -89,8 +88,8 @@ class TestSignatures(TestCase):
|
|||||||
|
|
||||||
def test_for(self):
|
def test_for(self):
|
||||||
# jedi-vim #11
|
# jedi-vim #11
|
||||||
self._run_simple("for sorted(", 'sorted', 0)
|
self._run_simple("for tuple(", 'tuple', 0)
|
||||||
self._run_simple("for s in sorted(", 'sorted', 0)
|
self._run_simple("for s in tuple(", 'tuple', 0)
|
||||||
|
|
||||||
|
|
||||||
def test_with(Script):
|
def test_with(Script):
|
||||||
@@ -272,7 +271,7 @@ def test_pow_params(Script):
|
|||||||
# See Github #1357.
|
# See Github #1357.
|
||||||
for sig in Script('pow(').get_signatures():
|
for sig in Script('pow(').get_signatures():
|
||||||
param_names = [p.name for p in sig.params]
|
param_names = [p.name for p in sig.params]
|
||||||
assert param_names in (['x', 'y'], ['x', 'y', 'z'])
|
assert param_names in (['base', 'exp'], ['base', 'exp', 'mod'])
|
||||||
|
|
||||||
|
|
||||||
def test_param_name(Script):
|
def test_param_name(Script):
|
||||||
|
|||||||
@@ -195,7 +195,7 @@ def test_hashlib_params(Script, environment):
|
|||||||
script = Script('from hashlib import sha256')
|
script = Script('from hashlib import sha256')
|
||||||
c, = script.complete()
|
c, = script.complete()
|
||||||
sig, = c.get_signatures()
|
sig, = c.get_signatures()
|
||||||
assert [p.name for p in sig.params] == ['arg']
|
assert [p.name for p in sig.params] == ['string']
|
||||||
|
|
||||||
|
|
||||||
def test_signature_params(Script):
|
def test_signature_params(Script):
|
||||||
@@ -619,7 +619,7 @@ def test_definition_goto_follow_imports(Script):
|
|||||||
|
|
||||||
('n = next; n', 'Union[next(__i: Iterator[_T]) -> _T, '
|
('n = next; n', 'Union[next(__i: Iterator[_T]) -> _T, '
|
||||||
'next(__i: Iterator[_T], default: _VT) -> Union[_T, _VT]]'),
|
'next(__i: Iterator[_T], default: _VT) -> Union[_T, _VT]]'),
|
||||||
('abs', 'abs(__n: SupportsAbs[_T]) -> _T'),
|
('abs', 'abs(__x: SupportsAbs[_T]) -> _T'),
|
||||||
('def foo(x, y): return x if xxxx else y\nfoo(str(), 1)\nfoo',
|
('def foo(x, y): return x if xxxx else y\nfoo(str(), 1)\nfoo',
|
||||||
'foo(x: str, y: int) -> Union[int, str]'),
|
'foo(x: str, y: int) -> Union[int, str]'),
|
||||||
('def foo(x, y = None): return x if xxxx else y\nfoo(str(), 1)\nfoo',
|
('def foo(x, y = None): return x if xxxx else y\nfoo(str(), 1)\nfoo',
|
||||||
|
|||||||
@@ -1,11 +1,16 @@
|
|||||||
from os.path import join, sep as s, dirname, expanduser
|
from os.path import join, sep as s, dirname, expanduser
|
||||||
import os
|
import os
|
||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
|
from itertools import count
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from ..helpers import root_dir
|
from ..helpers import root_dir
|
||||||
from jedi.api.helpers import _start_match, _fuzzy_match
|
from jedi.api.helpers import _start_match, _fuzzy_match
|
||||||
|
from jedi.inference.imports import _load_python_module
|
||||||
|
from jedi.file_io import KnownContentFileIO
|
||||||
|
from jedi.inference.base_value import ValueSet
|
||||||
|
|
||||||
|
|
||||||
def test_in_whitespace(Script):
|
def test_in_whitespace(Script):
|
||||||
@@ -150,19 +155,6 @@ def test_async(Script, environment):
|
|||||||
assert 'hey' in names
|
assert 'hey' in names
|
||||||
|
|
||||||
|
|
||||||
def test_method_doc_with_signature(Script):
|
|
||||||
code = 'f = open("")\nf.writelin'
|
|
||||||
c, = Script(code).complete()
|
|
||||||
assert c.name == 'writelines'
|
|
||||||
assert c.docstring() == 'writelines(lines: Iterable[AnyStr]) -> None'
|
|
||||||
|
|
||||||
|
|
||||||
def test_method_doc_with_signature2(Script):
|
|
||||||
code = 'f = open("")\nf.writelines'
|
|
||||||
d, = Script(code).goto()
|
|
||||||
assert d.docstring() == 'writelines(lines: Iterable[AnyStr]) -> None'
|
|
||||||
|
|
||||||
|
|
||||||
def test_with_stmt_error_recovery(Script):
|
def test_with_stmt_error_recovery(Script):
|
||||||
assert Script('with open('') as foo: foo.\na').complete(line=1)
|
assert Script('with open('') as foo: foo.\na').complete(line=1)
|
||||||
|
|
||||||
@@ -413,6 +405,22 @@ def test_ellipsis_completion(Script):
|
|||||||
assert Script('...').complete() == []
|
assert Script('...').complete() == []
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def module_injector():
|
||||||
|
counter = count()
|
||||||
|
|
||||||
|
def module_injector(inference_state, names, code):
|
||||||
|
assert isinstance(names, tuple)
|
||||||
|
file_io = KnownContentFileIO(
|
||||||
|
Path('foo/bar/module-injector-%s.py' % next(counter)).absolute(),
|
||||||
|
code
|
||||||
|
)
|
||||||
|
v = _load_python_module(inference_state, file_io, names)
|
||||||
|
inference_state.module_cache.add(names, ValueSet([v]))
|
||||||
|
|
||||||
|
return module_injector
|
||||||
|
|
||||||
|
|
||||||
def test_completion_cache(Script, module_injector):
|
def test_completion_cache(Script, module_injector):
|
||||||
"""
|
"""
|
||||||
For some modules like numpy, tensorflow or pandas we cache docstrings and
|
For some modules like numpy, tensorflow or pandas we cache docstrings and
|
||||||
@@ -449,3 +457,7 @@ def test_module_completions(Script, module):
|
|||||||
# Just make sure that there are no errors
|
# Just make sure that there are no errors
|
||||||
c.type
|
c.type
|
||||||
c.docstring()
|
c.docstring()
|
||||||
|
|
||||||
|
|
||||||
|
def test_whitespace_at_end_after_dot(Script):
|
||||||
|
assert 'strip' in [c.name for c in Script('str. ').complete()]
|
||||||
|
|||||||
@@ -37,6 +37,17 @@ def test_operator_doc(Script):
|
|||||||
assert len(d.docstring()) > 100
|
assert len(d.docstring()) > 100
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'code, help_part', [
|
||||||
|
('str', 'Create a new string object'),
|
||||||
|
('str.strip', 'Return a copy of the string'),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def test_stdlib_doc(Script, code, help_part):
|
||||||
|
h, = Script(code).help()
|
||||||
|
assert help_part in h.docstring(raw=True)
|
||||||
|
|
||||||
|
|
||||||
def test_lambda(Script):
|
def test_lambda(Script):
|
||||||
d, = Script('lambda x: x').help(column=0)
|
d, = Script('lambda x: x').help(column=0)
|
||||||
assert d.type == 'keyword'
|
assert d.type == 'keyword'
|
||||||
@@ -95,7 +106,7 @@ def test_builtin_docstring(goto_or_help_or_infer):
|
|||||||
d, = goto_or_help_or_infer('open')
|
d, = goto_or_help_or_infer('open')
|
||||||
|
|
||||||
doc = d.docstring()
|
doc = d.docstring()
|
||||||
assert doc.startswith('open(file: Union[')
|
assert doc.startswith('open(file: ')
|
||||||
assert 'Open file' in doc
|
assert 'Open file' in doc
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -599,6 +599,39 @@ def test_dict_getitem(code, types):
|
|||||||
assert [c.name for c in comps] == types
|
assert [c.name for c in comps] == types
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('class_is_findable', [False, True])
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'code, expected', [
|
||||||
|
('DunderCls()[0]', 'int'),
|
||||||
|
('dunder[0]', 'int'),
|
||||||
|
('next(DunderCls())', 'float'),
|
||||||
|
('next(dunder)', 'float'),
|
||||||
|
('for x in DunderCls(): x', 'str'),
|
||||||
|
#('for x in dunder: x', 'str'),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def test_dunders(class_is_findable, code, expected):
|
||||||
|
from typing import Iterator
|
||||||
|
|
||||||
|
class DunderCls:
|
||||||
|
def __getitem__(self, key) -> int:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __iter__(self, key) -> Iterator[str]:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __next__(self, key) -> float:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if not class_is_findable:
|
||||||
|
DunderCls.__name__ = 'asdf'
|
||||||
|
|
||||||
|
dunder = DunderCls()
|
||||||
|
|
||||||
|
n, = jedi.Interpreter(code, [locals()]).infer()
|
||||||
|
assert n.name == expected
|
||||||
|
|
||||||
|
|
||||||
def foo():
|
def foo():
|
||||||
raise KeyError
|
raise KeyError
|
||||||
|
|
||||||
@@ -678,3 +711,31 @@ def test_negate():
|
|||||||
assert x.name == 'int'
|
assert x.name == 'int'
|
||||||
value, = x._name.infer()
|
value, = x._name.infer()
|
||||||
assert value.get_safe_value() == -3
|
assert value.get_safe_value() == -3
|
||||||
|
|
||||||
|
|
||||||
|
def test_complete_not_findable_class_source():
|
||||||
|
class TestClass():
|
||||||
|
ta=1
|
||||||
|
ta1=2
|
||||||
|
|
||||||
|
# Simulate the environment where the class is defined in
|
||||||
|
# an interactive session and therefore inspect module
|
||||||
|
# cannot find its source code and raises OSError (Py 3.10+) or TypeError.
|
||||||
|
TestClass.__module__ = "__main__"
|
||||||
|
# There is a pytest __main__ module we have to remove temporarily.
|
||||||
|
module = sys.modules.pop("__main__")
|
||||||
|
try:
|
||||||
|
interpreter = jedi.Interpreter("TestClass.", [locals()])
|
||||||
|
completions = interpreter.complete(column=10, line=1)
|
||||||
|
finally:
|
||||||
|
sys.modules["__main__"] = module
|
||||||
|
|
||||||
|
assert "ta" in [c.name for c in completions]
|
||||||
|
assert "ta1" in [c.name for c in completions]
|
||||||
|
|
||||||
|
|
||||||
|
def test_param_infer_default():
|
||||||
|
abs_sig, = jedi.Interpreter('abs(', [{'abs': abs}]).get_signatures()
|
||||||
|
param, = abs_sig.params
|
||||||
|
assert param.name == 'x'
|
||||||
|
assert param.infer_default() == []
|
||||||
|
|||||||
@@ -189,3 +189,9 @@ def test_no_error(get_names):
|
|||||||
def test_is_side_effect(get_names, code, index, is_side_effect):
|
def test_is_side_effect(get_names, code, index, is_side_effect):
|
||||||
names = get_names(code, references=True, all_scopes=True)
|
names = get_names(code, references=True, all_scopes=True)
|
||||||
assert names[index].is_side_effect() == is_side_effect
|
assert names[index].is_side_effect() == is_side_effect
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_defined_names(get_names):
|
||||||
|
definition, = get_names("x = (1, 2)")
|
||||||
|
|
||||||
|
assert not definition.defined_names()
|
||||||
|
|||||||
@@ -68,6 +68,10 @@ def test_load_save_project(tmpdir):
|
|||||||
dict(all_scopes=True)),
|
dict(all_scopes=True)),
|
||||||
('some_search_test_var', ['test_api.test_project.test_search.some_search_test_var'],
|
('some_search_test_var', ['test_api.test_project.test_search.some_search_test_var'],
|
||||||
dict(complete=True, all_scopes=True)),
|
dict(complete=True, all_scopes=True)),
|
||||||
|
# Make sure that the searched name is not part of the file, by
|
||||||
|
# splitting it up.
|
||||||
|
('some_search_test_v' + 'a', ['test_api.test_project.test_search.some_search_test_var'],
|
||||||
|
dict(complete=True, all_scopes=True)),
|
||||||
|
|
||||||
('sample_int', ['helpers.sample_int'], {}),
|
('sample_int', ['helpers.sample_int'], {}),
|
||||||
('sample_int', ['helpers.sample_int'], dict(all_scopes=True)),
|
('sample_int', ['helpers.sample_int'], dict(all_scopes=True)),
|
||||||
@@ -146,7 +150,7 @@ def test_search(string, full_names, kwargs):
|
|||||||
defs = project.complete_search(string, **kwargs)
|
defs = project.complete_search(string, **kwargs)
|
||||||
else:
|
else:
|
||||||
defs = project.search(string, **kwargs)
|
defs = project.search(string, **kwargs)
|
||||||
assert sorted([('stub:' if d.is_stub() else '') + d.full_name for d in defs]) == full_names
|
assert sorted([('stub:' if d.is_stub() else '') + (d.full_name or d.name) for d in defs]) == full_names
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
|||||||
@@ -64,6 +64,6 @@ def test_wrong_encoding(Script, tmpdir):
|
|||||||
# Use both latin-1 and utf-8 (a really broken file).
|
# Use both latin-1 and utf-8 (a really broken file).
|
||||||
x.write_binary('foobar = 1\nä'.encode('latin-1') + 'ä'.encode('utf-8'))
|
x.write_binary('foobar = 1\nä'.encode('latin-1') + 'ä'.encode('utf-8'))
|
||||||
|
|
||||||
project = Project('.', sys_path=[tmpdir.strpath])
|
project = Project(tmpdir.strpath)
|
||||||
c, = Script('import x; x.foo', project=project).complete()
|
c, = Script('import x; x.foo', project=project).complete()
|
||||||
assert c.name == 'foobar'
|
assert c.name == 'foobar'
|
||||||
|
|||||||
@@ -10,16 +10,15 @@ def test_import_references(Script):
|
|||||||
|
|
||||||
def test_exclude_builtin_modules(Script):
|
def test_exclude_builtin_modules(Script):
|
||||||
def get(include):
|
def get(include):
|
||||||
from jedi.api.project import Project
|
references = Script(source).get_references(include_builtins=include)
|
||||||
script = Script(source, project=Project('', sys_path=[], smart_sys_path=False))
|
|
||||||
references = script.get_references(column=8, include_builtins=include)
|
|
||||||
return [(d.line, d.column) for d in references]
|
return [(d.line, d.column) for d in references]
|
||||||
source = '''import sys\nprint(sys.path)'''
|
source = '''import sys\nsys.setprofile'''
|
||||||
places = get(include=True)
|
places = get(include=True)
|
||||||
assert len(places) > 2 # Includes stubs
|
assert len(places) >= 3 # Includes stubs, the reference itself and the builtin
|
||||||
|
|
||||||
places = get(include=False)
|
places = get(include=False)
|
||||||
assert places == [(1, 7), (2, 6)]
|
# Just the reference
|
||||||
|
assert places == [(2, 4)]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('code, places', [
|
@pytest.mark.parametrize('code, places', [
|
||||||
|
|||||||
@@ -15,4 +15,4 @@ def test_module__file__(Script, environment):
|
|||||||
|
|
||||||
def_, = Script('import antigravity; antigravity.__file__').infer()
|
def_, = Script('import antigravity; antigravity.__file__').infer()
|
||||||
value = def_._name._value.get_safe_value()
|
value = def_._name._value.get_safe_value()
|
||||||
assert value.endswith('.py')
|
assert value.endswith('.pyi')
|
||||||
|
|||||||
@@ -507,3 +507,35 @@ def test_doctest_function_start(Script):
|
|||||||
return
|
return
|
||||||
''')
|
''')
|
||||||
assert Script(code).complete(7, 8)
|
assert Script(code).complete(7, 8)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"name, docstring", [
|
||||||
|
('prop1', 'Returns prop1.'),
|
||||||
|
('prop2', 'Returns None or ...'),
|
||||||
|
('prop3', 'Non-sense property.'),
|
||||||
|
('prop4', 'Django like property'),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def test_property(name, docstring, goto_or_complete):
|
||||||
|
code = dedent('''
|
||||||
|
from typing import Optional
|
||||||
|
class Test:
|
||||||
|
@property
|
||||||
|
def prop1(self) -> int:
|
||||||
|
"""Returns prop1."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def prop2(self) -> Optional[int]:
|
||||||
|
"""Returns None or ..."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def prop3(self) -> None:
|
||||||
|
"""Non-sense property."""
|
||||||
|
|
||||||
|
@cached_property # Not imported, but Jedi uses a heuristic
|
||||||
|
def prop4(self) -> None:
|
||||||
|
"""Django like property"""
|
||||||
|
''')
|
||||||
|
n, = goto_or_complete(code + 'Test().' + name)
|
||||||
|
assert n.docstring() == docstring
|
||||||
|
|||||||
@@ -13,17 +13,15 @@ def test_completions(Script):
|
|||||||
assert len(s.complete()) >= 15
|
assert len(s.complete()) >= 15
|
||||||
|
|
||||||
|
|
||||||
def test_get_signatures_extension(Script):
|
def test_get_signatures_extension(Script, environment):
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
func = 'LoadLibrary'
|
func = 'LoadLibrary'
|
||||||
params = 1
|
|
||||||
else:
|
else:
|
||||||
func = 'dlopen'
|
func = 'dlopen'
|
||||||
params = 2
|
|
||||||
s = Script('import _ctypes; _ctypes.%s(' % (func,))
|
s = Script('import _ctypes; _ctypes.%s(' % (func,))
|
||||||
sigs = s.get_signatures()
|
sigs = s.get_signatures()
|
||||||
assert len(sigs) == 1
|
assert len(sigs) == 1
|
||||||
assert len(sigs[0].params) == params
|
assert len(sigs[0].params) in (1, 2)
|
||||||
|
|
||||||
|
|
||||||
def test_get_signatures_stdlib(Script):
|
def test_get_signatures_stdlib(Script):
|
||||||
|
|||||||
@@ -69,11 +69,12 @@ def test_stub_get_line_code(Script):
|
|||||||
code = 'from abc import ABC; ABC'
|
code = 'from abc import ABC; ABC'
|
||||||
script = Script(code)
|
script = Script(code)
|
||||||
d, = script.goto(only_stubs=True)
|
d, = script.goto(only_stubs=True)
|
||||||
assert d.get_line_code() == 'class ABC(metaclass=ABCMeta): ...\n'
|
# Replace \r for tests on Windows
|
||||||
|
assert d.get_line_code().replace('\r', '') == 'class ABC(metaclass=ABCMeta): ...\n'
|
||||||
del parser_cache[script._inference_state.latest_grammar._hashed][d.module_path]
|
del parser_cache[script._inference_state.latest_grammar._hashed][d.module_path]
|
||||||
d, = Script(path=d.module_path).goto(d.line, d.column, only_stubs=True)
|
d, = Script(path=d.module_path).goto(d.line, d.column, only_stubs=True)
|
||||||
assert d.is_stub()
|
assert d.is_stub()
|
||||||
assert d.get_line_code() == 'class ABC(metaclass=ABCMeta): ...\n'
|
assert d.get_line_code().replace('\r', '') == 'class ABC(metaclass=ABCMeta): ...\n'
|
||||||
|
|
||||||
|
|
||||||
def test_os_stat_result(Script):
|
def test_os_stat_result(Script):
|
||||||
|
|||||||
@@ -21,10 +21,10 @@ def test_get_typeshed_directories():
|
|||||||
def transform(set_):
|
def transform(set_):
|
||||||
return {x.replace('/', os.path.sep) for x in set_}
|
return {x.replace('/', os.path.sep) for x in set_}
|
||||||
|
|
||||||
dirs = get_dirs(PythonVersionInfo(3, 6))
|
dirs = get_dirs(PythonVersionInfo(3, 7))
|
||||||
assert dirs == transform({'stdlib/2and3', 'stdlib/3',
|
assert dirs == transform({'stdlib/2and3', 'stdlib/3', 'stdlib/3.7',
|
||||||
'stdlib/3.6', 'third_party/2and3',
|
'third_party/2and3',
|
||||||
'third_party/3', 'third_party/3.6'})
|
'third_party/3', 'third_party/3.7'})
|
||||||
|
|
||||||
|
|
||||||
def test_get_stub_files():
|
def test_get_stub_files():
|
||||||
@@ -92,7 +92,7 @@ def test_sys_exc_info(Script):
|
|||||||
# It's an optional.
|
# It's an optional.
|
||||||
assert def_.name == 'BaseException'
|
assert def_.name == 'BaseException'
|
||||||
assert def_.module_path == typeshed.TYPESHED_PATH.joinpath(
|
assert def_.module_path == typeshed.TYPESHED_PATH.joinpath(
|
||||||
'stdlib', '2and3', 'builtins.pyi'
|
'stdlib', '3', 'builtins.pyi'
|
||||||
)
|
)
|
||||||
assert def_.type == 'instance'
|
assert def_.type == 'instance'
|
||||||
assert none.name == 'NoneType'
|
assert none.name == 'NoneType'
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ def test_implicit_nested_namespace_package(Script):
|
|||||||
assert len(result) == 1
|
assert len(result) == 1
|
||||||
|
|
||||||
implicit_pkg, = Script(code, project=project).infer(column=10)
|
implicit_pkg, = Script(code, project=project).infer(column=10)
|
||||||
assert implicit_pkg.type == 'module'
|
assert implicit_pkg.type == 'namespace'
|
||||||
assert implicit_pkg.module_path is None
|
assert implicit_pkg.module_path is None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -101,6 +101,16 @@ def test_correct_zip_package_behavior(Script, inference_state, environment, code
|
|||||||
assert value.py__package__() == []
|
assert value.py__package__() == []
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("code,names", [
|
||||||
|
("from pkg.", {"module", "nested", "namespace"}),
|
||||||
|
("from pkg.nested.", {"nested_module"})
|
||||||
|
])
|
||||||
|
def test_zip_package_import_complete(Script, environment, code, names):
|
||||||
|
sys_path = environment.get_sys_path() + [str(pkg_zip_path)]
|
||||||
|
completions = Script(code, project=Project('.', sys_path=sys_path)).complete()
|
||||||
|
assert names == {c.name for c in completions}
|
||||||
|
|
||||||
|
|
||||||
def test_find_module_not_package_zipped(Script, inference_state, environment):
|
def test_find_module_not_package_zipped(Script, inference_state, environment):
|
||||||
path = get_example_dir('zipped_imports', 'not_pkg.zip')
|
path = get_example_dir('zipped_imports', 'not_pkg.zip')
|
||||||
sys_path = environment.get_sys_path() + [path]
|
sys_path = environment.get_sys_path() + [path]
|
||||||
@@ -324,12 +334,13 @@ def test_compiled_import_none(monkeypatch, Script):
|
|||||||
# context that was initially given, but now we just work with the file
|
# context that was initially given, but now we just work with the file
|
||||||
# system.
|
# system.
|
||||||
(os.path.join(THIS_DIR, 'test_docstring.py'), False,
|
(os.path.join(THIS_DIR, 'test_docstring.py'), False,
|
||||||
('test', 'test_inference', 'test_imports')),
|
('test_inference', 'test_imports')),
|
||||||
(os.path.join(THIS_DIR, '__init__.py'), True,
|
(os.path.join(THIS_DIR, '__init__.py'), True,
|
||||||
('test', 'test_inference', 'test_imports')),
|
('test_inference', 'test_imports')),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
def test_get_modules_containing_name(inference_state, path, goal, is_package):
|
def test_get_modules_containing_name(inference_state, path, goal, is_package):
|
||||||
|
inference_state.project = Project(test_dir)
|
||||||
module = imports._load_python_module(
|
module = imports._load_python_module(
|
||||||
inference_state,
|
inference_state,
|
||||||
FileIO(path),
|
FileIO(path),
|
||||||
|
|||||||
@@ -102,6 +102,25 @@ class X:
|
|||||||
(partialmethod_code + 'X().d(', None),
|
(partialmethod_code + 'X().d(', None),
|
||||||
(partialmethod_code + 'X.c(', 'func(a, b)'),
|
(partialmethod_code + 'X.c(', 'func(a, b)'),
|
||||||
(partialmethod_code + 'X.d(', None),
|
(partialmethod_code + 'X.d(', None),
|
||||||
|
|
||||||
|
('import contextlib\n@contextlib.contextmanager\ndef f(x): pass\nf(', 'f(x)'),
|
||||||
|
|
||||||
|
# typing lib
|
||||||
|
('from typing import cast\ncast(', {
|
||||||
|
'cast(typ: object, val: Any) -> Any',
|
||||||
|
'cast(typ: str, val: Any) -> Any',
|
||||||
|
'cast(typ: Type[_T], val: Any) -> _T'}),
|
||||||
|
('from typing import TypeVar\nTypeVar(',
|
||||||
|
'TypeVar(name: str, *constraints: Type[Any], bound: Union[None, Type[Any], str]=..., '
|
||||||
|
'covariant: bool=..., contravariant: bool=...)'),
|
||||||
|
('from typing import List\nList(', None),
|
||||||
|
('from typing import List\nList[int](', None),
|
||||||
|
('from typing import Tuple\nTuple(', None),
|
||||||
|
('from typing import Tuple\nTuple[int](', None),
|
||||||
|
('from typing import Optional\nOptional(', None),
|
||||||
|
('from typing import Optional\nOptional[int](', None),
|
||||||
|
('from typing import Any\nAny(', None),
|
||||||
|
('from typing import NewType\nNewType(', 'NewType(name: str, tp: Type[_T]) -> Type[_T]'),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
def test_tree_signature(Script, environment, code, expected):
|
def test_tree_signature(Script, environment, code, expected):
|
||||||
@@ -112,8 +131,10 @@ def test_tree_signature(Script, environment, code, expected):
|
|||||||
if expected is None:
|
if expected is None:
|
||||||
assert not Script(code).get_signatures()
|
assert not Script(code).get_signatures()
|
||||||
else:
|
else:
|
||||||
sig, = Script(code).get_signatures()
|
actual = {sig.to_string() for sig in Script(code).get_signatures()}
|
||||||
assert expected == sig.to_string()
|
if not isinstance(expected, set):
|
||||||
|
expected = {expected}
|
||||||
|
assert expected == actual
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
@@ -134,7 +155,7 @@ def test_tree_signature(Script, environment, code, expected):
|
|||||||
('full_redirect(C)', 'z, *, c'),
|
('full_redirect(C)', 'z, *, c'),
|
||||||
('full_redirect(C())', 'y'),
|
('full_redirect(C())', 'y'),
|
||||||
('full_redirect(G)', 't: T'),
|
('full_redirect(G)', 't: T'),
|
||||||
('full_redirect(G[str])', 't: T'),
|
('full_redirect(G[str])', '*args, **kwargs'),
|
||||||
('D', 'D(a, z, /)'),
|
('D', 'D(a, z, /)'),
|
||||||
('D()', 'D(x, y)'),
|
('D()', 'D(x, y)'),
|
||||||
('D().foo', 'foo(a, *, bar, z, **kwargs)'),
|
('D().foo', 'foo(a, *, bar, z, **kwargs)'),
|
||||||
@@ -225,32 +246,40 @@ def test_nested_signatures(Script, environment, combination, expected):
|
|||||||
assert expected == computed
|
assert expected == computed
|
||||||
|
|
||||||
|
|
||||||
def test_pow_signature(Script):
|
def test_pow_signature(Script, environment):
|
||||||
# See github #1357
|
# See github #1357
|
||||||
sigs = Script('pow(').get_signatures()
|
sigs = Script('pow(').get_signatures()
|
||||||
strings = {sig.to_string() for sig in sigs}
|
strings = {sig.to_string() for sig in sigs}
|
||||||
assert strings == {'pow(x: float, y: float, z: float, /) -> float',
|
if environment.version_info < (3, 8):
|
||||||
'pow(x: float, y: float, /) -> float',
|
assert strings == {'pow(base: _SupportsPow2[_E, _T_co], exp: _E, /) -> _T_co',
|
||||||
'pow(x: int, y: int, z: int, /) -> Any',
|
'pow(base: _SupportsPow3[_E, _M, _T_co], exp: _E, mod: _M, /) -> _T_co',
|
||||||
'pow(x: int, y: int, /) -> Any'}
|
'pow(base: float, exp: float, mod: None=..., /) -> float',
|
||||||
|
'pow(base: int, exp: int, mod: None=..., /) -> Any',
|
||||||
|
'pow(base: int, exp: int, mod: int, /) -> int'}
|
||||||
|
else:
|
||||||
|
assert strings == {'pow(base: _SupportsPow2[_E, _T_co], exp: _E) -> _T_co',
|
||||||
|
'pow(base: _SupportsPow3[_E, _M, _T_co], exp: _E, mod: _M) -> _T_co',
|
||||||
|
'pow(base: float, exp: float, mod: None=...) -> float',
|
||||||
|
'pow(base: int, exp: int, mod: None=...) -> Any',
|
||||||
|
'pow(base: int, exp: int, mod: int) -> int'}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
'code, signature', [
|
'code, signature', [
|
||||||
[dedent('''
|
[dedent('''
|
||||||
|
# identifier:A
|
||||||
import functools
|
import functools
|
||||||
def f(x):
|
def f(x):
|
||||||
pass
|
pass
|
||||||
def x(f):
|
def x(f):
|
||||||
@functools.wraps(f)
|
@functools.wraps(f)
|
||||||
def wrapper(*args):
|
def wrapper(*args):
|
||||||
# Have no arguments here, but because of wraps, the signature
|
|
||||||
# should still be f's.
|
|
||||||
return f(*args)
|
return f(*args)
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
x(f)('''), 'f(x, /)'],
|
x(f)('''), 'f(x, /)'],
|
||||||
[dedent('''
|
[dedent('''
|
||||||
|
# identifier:B
|
||||||
import functools
|
import functools
|
||||||
def f(x):
|
def f(x):
|
||||||
pass
|
pass
|
||||||
@@ -263,6 +292,26 @@ def test_pow_signature(Script):
|
|||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
x(f)('''), 'f()'],
|
x(f)('''), 'f()'],
|
||||||
|
[dedent('''
|
||||||
|
# identifier:C
|
||||||
|
import functools
|
||||||
|
def f(x: int, y: float):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@functools.wraps(f)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
return f(*args, **kwargs)
|
||||||
|
|
||||||
|
wrapper('''), 'f(x: int, y: float)'],
|
||||||
|
[dedent('''
|
||||||
|
# identifier:D
|
||||||
|
def f(x: int, y: float):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
return f(*args, **kwargs)
|
||||||
|
|
||||||
|
wrapper('''), 'wrapper(x: int, y: float)'],
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
def test_wraps_signature(Script, code, signature):
|
def test_wraps_signature(Script, code, signature):
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
|
import gc
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from jedi import parser_utils
|
from jedi import parser_utils
|
||||||
from parso import parse
|
from parso import parse
|
||||||
|
from parso.cache import parser_cache
|
||||||
from parso.python import tree
|
from parso.python import tree
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@@ -67,3 +71,18 @@ def test_get_signature(code, signature):
|
|||||||
if node.type == 'simple_stmt':
|
if node.type == 'simple_stmt':
|
||||||
node = node.children[0]
|
node = node.children[0]
|
||||||
assert parser_utils.get_signature(node) == signature
|
assert parser_utils.get_signature(node) == signature
|
||||||
|
|
||||||
|
|
||||||
|
def test_parser_cache_clear(Script):
|
||||||
|
"""
|
||||||
|
If parso clears its cache, Jedi should not keep those resources, they
|
||||||
|
should be freed.
|
||||||
|
"""
|
||||||
|
script = Script("a = abs\na", path=Path(__file__).parent / 'parser_cache_test_foo.py')
|
||||||
|
script.complete()
|
||||||
|
module_id = id(script._module_node)
|
||||||
|
del parser_cache[script._inference_state.grammar._hashed][script.path]
|
||||||
|
del script
|
||||||
|
|
||||||
|
gc.collect()
|
||||||
|
assert module_id not in [id(m) for m in gc.get_referrers(tree.Module)]
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from jedi import settings
|
from jedi import settings
|
||||||
from jedi.inference.names import ValueName
|
|
||||||
from jedi.inference.compiled import CompiledValueName
|
from jedi.inference.compiled import CompiledValueName
|
||||||
from jedi.inference.gradual.typeshed import StubModuleValue
|
from jedi.inference.compiled.value import CompiledModule
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
@@ -13,9 +12,9 @@ def auto_import_json(monkeypatch):
|
|||||||
|
|
||||||
def test_base_auto_import_modules(auto_import_json, Script):
|
def test_base_auto_import_modules(auto_import_json, Script):
|
||||||
loads, = Script('import json; json.loads').infer()
|
loads, = Script('import json; json.loads').infer()
|
||||||
assert isinstance(loads._name, ValueName)
|
assert isinstance(loads._name, CompiledValueName)
|
||||||
value, = loads._name.infer()
|
value, = loads._name.infer()
|
||||||
assert isinstance(value.parent_context._value, StubModuleValue)
|
assert isinstance(value.parent_context._value, CompiledModule)
|
||||||
|
|
||||||
|
|
||||||
def test_auto_import_modules_imports(auto_import_json, Script):
|
def test_auto_import_modules_imports(auto_import_json, Script):
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ class TestSetupReadline(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
# There are quite a few differences, because both Windows and Linux
|
# There are quite a few differences, because both Windows and Linux
|
||||||
# (posix and nt) librariesare included.
|
# (posix and nt) librariesare included.
|
||||||
assert len(difference) < 15
|
assert len(difference) < 30
|
||||||
|
|
||||||
def test_local_import(self):
|
def test_local_import(self):
|
||||||
s = 'import test.test_utils'
|
s = 'import test.test_utils'
|
||||||
|
|||||||
Reference in New Issue
Block a user