mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 14:04:26 +08:00
Compare commits
126 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
41e9e957e7 | ||
|
|
b225678a42 | ||
|
|
30adf43a89 | ||
|
|
be6df62434 | ||
|
|
e53359ad88 | ||
|
|
6e5d5b779c | ||
|
|
91ffdead32 | ||
|
|
2859e4f409 | ||
|
|
8ee4c26ae4 | ||
|
|
4d09ac07e4 | ||
|
|
82d1902f38 | ||
|
|
857c9be500 | ||
|
|
e839683e91 | ||
|
|
255186376e | ||
|
|
a67deeb602 | ||
|
|
d543d1d004 | ||
|
|
9d18b7c36d | ||
|
|
340dedd021 | ||
|
|
fff6e0ce2e | ||
|
|
473b35e6ec | ||
|
|
a0527a5af5 | ||
|
|
bbbaad21e8 | ||
|
|
ee90cd97b6 | ||
|
|
68e435cc66 | ||
|
|
b69d4d87c3 | ||
|
|
0fcb4468e7 | ||
|
|
5c578e1899 | ||
|
|
9bad42c0db | ||
|
|
3118462a93 | ||
|
|
065580b5d4 | ||
|
|
39c8317922 | ||
|
|
ab97e9f784 | ||
|
|
f7c9ee9433 | ||
|
|
8792c6d432 | ||
|
|
a4574a50d0 | ||
|
|
f11014fc5d | ||
|
|
54a6dadde3 | ||
|
|
740b474eda | ||
|
|
950ce70239 | ||
|
|
6982a49977 | ||
|
|
9b8cece7ef | ||
|
|
162034b387 | ||
|
|
7494c9495e | ||
|
|
7d77f61040 | ||
|
|
11280ef502 | ||
|
|
94ec4b873a | ||
|
|
f8e502f90c | ||
|
|
dc20f2e5a0 | ||
|
|
4e175ca82b | ||
|
|
30e9b7b5be | ||
|
|
83545bc9ec | ||
|
|
57e7b83455 | ||
|
|
0770372857 | ||
|
|
7e533ca7e1 | ||
|
|
a60fdba1d4 | ||
|
|
9d399a9229 | ||
|
|
770cdade00 | ||
|
|
29890c1f29 | ||
|
|
159566e1a0 | ||
|
|
a80618a2df | ||
|
|
4bc1b6ef99 | ||
|
|
d655d65d3a | ||
|
|
51f4a99a1e | ||
|
|
93c14d2e6e | ||
|
|
57aefed6ea | ||
|
|
8a4b079d0f | ||
|
|
62cbcb0844 | ||
|
|
d8420d0f72 | ||
|
|
886279fb6d | ||
|
|
ff3a7f367f | ||
|
|
1f70e3301e | ||
|
|
a34c348a55 | ||
|
|
972123c9c9 | ||
|
|
6455a14841 | ||
|
|
8d9e3ab3a7 | ||
|
|
048173e467 | ||
|
|
1947e7dd56 | ||
|
|
01d8da8f73 | ||
|
|
6ea5ad7b19 | ||
|
|
cd4ca74d7a | ||
|
|
67d6262f45 | ||
|
|
5f19237a3e | ||
|
|
f2444b4be5 | ||
|
|
7028bbb5d5 | ||
|
|
3699ba0aa7 | ||
|
|
72d34f3d7d | ||
|
|
a28bd24bef | ||
|
|
54cb64292c | ||
|
|
d421b920fa | ||
|
|
c137eb6918 | ||
|
|
d67facc922 | ||
|
|
7023b645b1 | ||
|
|
b5120cc90b | ||
|
|
483e78993d | ||
|
|
3dbcd2c6de | ||
|
|
ca36fcfa4b | ||
|
|
825c6b93bf | ||
|
|
c22585c6f2 | ||
|
|
431d1e104d | ||
|
|
adcd6ade8b | ||
|
|
32a1dd33a6 | ||
|
|
9ea01bcc69 | ||
|
|
77cfefc1cc | ||
|
|
ff7d6c6e4c | ||
|
|
6ee33bd385 | ||
|
|
0fbc2aafa3 | ||
|
|
fe7e350051 | ||
|
|
b814ca2951 | ||
|
|
aae2a8e3ed | ||
|
|
67e0bec597 | ||
|
|
c71e06fcb3 | ||
|
|
bbd5bcf3ca | ||
|
|
d888c1b266 | ||
|
|
83d0e23800 | ||
|
|
dc4e48d7c7 | ||
|
|
664b10a5c6 | ||
|
|
36a4b7d48c | ||
|
|
b0025ee6ba | ||
|
|
fac0b7f068 | ||
|
|
aeadba7cad | ||
|
|
fd0e6aed96 | ||
|
|
c89fa8e927 | ||
|
|
00e23ddcee | ||
|
|
66e97e5b93 | ||
|
|
0f5ea3de5f | ||
|
|
e47bbbb851 |
22
.github/workflows/ci.yml
vendored
22
.github/workflows/ci.yml
vendored
@@ -1,5 +1,5 @@
|
||||
name: ci
|
||||
on: [push, pull_request]
|
||||
on: [push, pull_request, workflow_dispatch]
|
||||
|
||||
jobs:
|
||||
tests:
|
||||
@@ -7,22 +7,24 @@ jobs:
|
||||
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']
|
||||
python-version: ["3.13", "3.12", "3.11", "3.10", "3.9", "3.8", "3.7", "3.6"]
|
||||
environment: ['3.8', '3.13', '3.12', '3.11', '3.10', '3.9', '3.7', '3.6', 'interpreter']
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
- uses: actions/setup-python@v2
|
||||
- uses: actions/setup-python@v5
|
||||
if: ${{ matrix.environment != 'interpreter' }}
|
||||
with:
|
||||
python-version: ${{ matrix.environment }}
|
||||
allow-prereleases: true
|
||||
|
||||
- uses: actions/setup-python@v2
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: ${{ matrix.python-version }}
|
||||
allow-prereleases: true
|
||||
|
||||
- name: Install dependencies
|
||||
run: 'pip install .[testing]'
|
||||
@@ -36,7 +38,7 @@ jobs:
|
||||
runs-on: ubuntu-20.04
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
@@ -45,15 +47,15 @@ jobs:
|
||||
|
||||
- name: Run tests
|
||||
run: |
|
||||
python -m flake8 jedi setup.py
|
||||
python -m mypy jedi sith.py
|
||||
python -m flake8 jedi test setup.py
|
||||
python -m mypy jedi sith.py setup.py
|
||||
|
||||
coverage:
|
||||
runs-on: ubuntu-20.04
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
|
||||
@@ -9,3 +9,8 @@ python:
|
||||
|
||||
submodules:
|
||||
include: all
|
||||
|
||||
build:
|
||||
os: ubuntu-22.04
|
||||
tools:
|
||||
python: "3.11"
|
||||
|
||||
@@ -63,6 +63,9 @@ Code Contributors
|
||||
- Leo Ryu (@Leo-Ryu)
|
||||
- Joseph Birkner (@josephbirkner)
|
||||
- Márcio Mazza (@marciomazza)
|
||||
- Martin Vielsmaier (@moser) <martin@vielsmaier.net>
|
||||
- TingJia Wu (@WutingjiaX) <wutingjia@bytedance.com>
|
||||
- Nguyễn Hồng Quân <ng.hong.quan@gmail.com>
|
||||
|
||||
And a few more "anonymous" contributors.
|
||||
|
||||
|
||||
@@ -6,6 +6,25 @@ Changelog
|
||||
Unreleased
|
||||
++++++++++
|
||||
|
||||
0.19.2 (2024-11-10)
|
||||
+++++++++++++++++++
|
||||
|
||||
- Python 3.13 support
|
||||
|
||||
0.19.1 (2023-10-02)
|
||||
+++++++++++++++++++
|
||||
|
||||
- Python 3.12 support (Thanks Peter!)
|
||||
|
||||
0.19.0 (2023-07-29)
|
||||
+++++++++++++++++++
|
||||
|
||||
- Python 3.11 support
|
||||
- Massive improvements in performance for ``Interpreter`` (e.g. IPython) users.
|
||||
This especially affects ``pandas`` users with large datasets.
|
||||
- Add ``jedi.settings.allow_unsafe_interpreter_executions`` to make it easier
|
||||
for IPython users to avoid unsafe executions.
|
||||
|
||||
0.18.2 (2022-11-21)
|
||||
+++++++++++++++++++
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ Jedi can currently be used with the following editors/projects:
|
||||
- `GNOME Builder`_ (with support for GObject Introspection)
|
||||
- Gedit (gedi_)
|
||||
- wdb_ - Web Debugger
|
||||
- `Eric IDE`_ (Available as a plugin)
|
||||
- `Eric IDE`_
|
||||
- `IPython 6.0.0+ <https://ipython.readthedocs.io/en/stable/whatsnew/version6.html>`_
|
||||
- `xonsh shell <https://xon.sh/contents.html>`_ has `jedi extension <https://xon.sh/xontribs.html#jedi>`_
|
||||
|
||||
@@ -51,7 +51,8 @@ and many more!
|
||||
There are a few language servers that use Jedi:
|
||||
|
||||
- `jedi-language-server <https://github.com/pappasam/jedi-language-server>`_
|
||||
- `python-language-server <https://github.com/palantir/python-language-server>`_
|
||||
- `python-language-server <https://github.com/palantir/python-language-server>`_ (currently unmaintained)
|
||||
- `python-lsp-server <https://github.com/python-lsp/python-lsp-server>`_ (fork from python-language-server)
|
||||
- `anakin-language-server <https://github.com/muffinmad/anakin-language-server>`_
|
||||
|
||||
Here are some pictures taken from jedi-vim_:
|
||||
|
||||
9
SECURITY.md
Normal file
9
SECURITY.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# Security Policy
|
||||
|
||||
If security issues arise, we will try to fix those as soon as possible.
|
||||
|
||||
Due to Jedi's nature, Security Issues will probably be extremely rare, but we will of course treat them seriously.
|
||||
|
||||
## Reporting Security Problems
|
||||
|
||||
If you need to report a security vulnerability, please send an email to davidhalter88@gmail.com. Typically, I will respond in the next few business days.
|
||||
@@ -35,7 +35,7 @@ to write my own version of a completion engine.
|
||||
The first idea was to execute non-dangerous code. But I soon realized, that
|
||||
this would not work. So I started to build a static analysis tool.
|
||||
The biggest problem that I had at the time was that I did not know a thing
|
||||
about parsers.I did not did not even know the word static analysis. It turns
|
||||
about parsers. I did not even know the word static analysis. It turns
|
||||
out they are the foundation of a good static analysis tool. I of course did not
|
||||
know that and tried to write my own poor version of a parser that I ended up
|
||||
throwing away two years later.
|
||||
@@ -53,7 +53,7 @@ quick and is pretty much feature complete.
|
||||
|
||||
--------
|
||||
|
||||
I will leave you with a small annectote that happend in 2012, if I remember
|
||||
I will leave you with a small anecdote that happened in 2012, if I remember
|
||||
correctly. After I explained Guido van Rossum, how some parts of my
|
||||
auto-completion work, he said:
|
||||
|
||||
|
||||
@@ -77,7 +77,7 @@ Performance Issues
|
||||
|
||||
Importing ``numpy`` can be quite slow sometimes, as well as loading the
|
||||
builtins the first time. If you want to speed things up, you could preload
|
||||
libriaries in |jedi|, with :func:`.preload_module`. However, once loaded, this
|
||||
libraries in |jedi|, with :func:`.preload_module`. However, once loaded, this
|
||||
should not be a problem anymore. The same is true for huge modules like
|
||||
``PySide``, ``wx``, ``tensorflow``, ``pandas``, etc.
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ Using Jedi
|
||||
==========
|
||||
|
||||
|jedi| is can be used with a variety of :ref:`plugins <editor-plugins>`,
|
||||
`language servers <language-servers>` and other software.
|
||||
:ref:`language servers <language-servers>` and other software.
|
||||
It is also possible to use |jedi| in the :ref:`Python shell or with IPython
|
||||
<repl-completion>`.
|
||||
|
||||
@@ -16,7 +16,8 @@ Language Servers
|
||||
--------------
|
||||
|
||||
- `jedi-language-server <https://github.com/pappasam/jedi-language-server>`_
|
||||
- `python-language-server <https://github.com/palantir/python-language-server>`_
|
||||
- `python-language-server <https://github.com/palantir/python-language-server>`_ (currently unmaintained)
|
||||
- `python-lsp-server <https://github.com/python-lsp/python-lsp-server>`_ (fork from python-language-server)
|
||||
- `anakin-language-server <https://github.com/muffinmad/anakin-language-server>`_
|
||||
|
||||
.. _editor-plugins:
|
||||
@@ -86,7 +87,7 @@ Gedit
|
||||
Eric IDE
|
||||
~~~~~~~~
|
||||
|
||||
- `Eric IDE`_ (Available as a plugin)
|
||||
- `Eric IDE`_
|
||||
|
||||
Web Debugger
|
||||
~~~~~~~~~~~~
|
||||
|
||||
@@ -27,7 +27,7 @@ ad
|
||||
load
|
||||
"""
|
||||
|
||||
__version__ = '0.18.2'
|
||||
__version__ = '0.19.2'
|
||||
|
||||
from jedi.api import Script, Interpreter, set_debug_function, preload_module
|
||||
from jedi import settings
|
||||
|
||||
@@ -5,11 +5,24 @@ different Python versions.
|
||||
import errno
|
||||
import sys
|
||||
import pickle
|
||||
from typing import Any
|
||||
|
||||
|
||||
class Unpickler(pickle.Unpickler):
|
||||
def find_class(self, module: str, name: str) -> Any:
|
||||
# Python 3.13 moved pathlib implementation out of __init__.py as part of
|
||||
# generalising its implementation. Ensure that we support loading
|
||||
# pickles from 3.13 on older version of Python. Since 3.13 maintained a
|
||||
# compatible API, pickles from older Python work natively on the newer
|
||||
# version.
|
||||
if module == 'pathlib._local':
|
||||
module = 'pathlib'
|
||||
return super().find_class(module, name)
|
||||
|
||||
|
||||
def pickle_load(file):
|
||||
try:
|
||||
return pickle.load(file)
|
||||
return Unpickler(file).load()
|
||||
# Python on Windows don't throw EOF errors for pipes. So reraise them with
|
||||
# the correct type, which is caught upwards.
|
||||
except OSError:
|
||||
|
||||
@@ -206,6 +206,7 @@ class Script:
|
||||
before magic methods and name mangled names that start with ``__``.
|
||||
:rtype: list of :class:`.Completion`
|
||||
"""
|
||||
self._inference_state.reset_recursion_limitations()
|
||||
with debug.increase_indent_cm('complete'):
|
||||
completion = Completion(
|
||||
self._inference_state, self._get_module_context(), self._code_lines,
|
||||
@@ -230,6 +231,7 @@ class Script:
|
||||
:param prefer_stubs: Prefer stubs to Python objects for this method.
|
||||
:rtype: list of :class:`.Name`
|
||||
"""
|
||||
self._inference_state.reset_recursion_limitations()
|
||||
pos = line, column
|
||||
leaf = self._module_node.get_name_of_position(pos)
|
||||
if leaf is None:
|
||||
@@ -273,6 +275,7 @@ class Script:
|
||||
:param prefer_stubs: Prefer stubs to Python objects for this method.
|
||||
:rtype: list of :class:`.Name`
|
||||
"""
|
||||
self._inference_state.reset_recursion_limitations()
|
||||
tree_name = self._module_node.get_name_of_position((line, column))
|
||||
if tree_name is None:
|
||||
# Without a name we really just want to jump to the result e.g.
|
||||
@@ -365,10 +368,17 @@ class Script:
|
||||
|
||||
:rtype: list of :class:`.Name`
|
||||
"""
|
||||
self._inference_state.reset_recursion_limitations()
|
||||
definitions = self.goto(line, column, follow_imports=True)
|
||||
if definitions:
|
||||
return definitions
|
||||
leaf = self._module_node.get_leaf_for_position((line, column))
|
||||
|
||||
if leaf is not None and leaf.end_pos == (line, column) and leaf.type == 'newline':
|
||||
next_ = leaf.get_next_leaf()
|
||||
if next_ is not None and next_.start_pos == leaf.end_pos:
|
||||
leaf = next_
|
||||
|
||||
if leaf is not None and leaf.type in ('keyword', 'operator', 'error_leaf'):
|
||||
def need_pydoc():
|
||||
if leaf.value in ('(', ')', '[', ']'):
|
||||
@@ -400,6 +410,7 @@ class Script:
|
||||
the current module only.
|
||||
:rtype: list of :class:`.Name`
|
||||
"""
|
||||
self._inference_state.reset_recursion_limitations()
|
||||
|
||||
def _references(include_builtins=True, scope='project'):
|
||||
if scope not in ('project', 'file'):
|
||||
@@ -434,6 +445,7 @@ class Script:
|
||||
|
||||
:rtype: list of :class:`.Signature`
|
||||
"""
|
||||
self._inference_state.reset_recursion_limitations()
|
||||
pos = line, column
|
||||
call_details = helpers.get_signature_details(self._module_node, pos)
|
||||
if call_details is None:
|
||||
@@ -553,6 +565,7 @@ class Script:
|
||||
return parso_to_jedi_errors(self._inference_state.grammar, self._module_node)
|
||||
|
||||
def _names(self, all_scopes=False, definitions=True, references=False):
|
||||
self._inference_state.reset_recursion_limitations()
|
||||
# Set line/column to a random position, because they don't matter.
|
||||
module_context = self._get_module_context()
|
||||
defs = [
|
||||
@@ -708,7 +721,6 @@ class Interpreter(Script):
|
||||
:param namespaces: A list of namespace dictionaries such as the one
|
||||
returned by :func:`globals` and :func:`locals`.
|
||||
"""
|
||||
_allow_descriptor_getattr_default = True
|
||||
|
||||
def __init__(self, code, namespaces, *, project=None, **kwds):
|
||||
try:
|
||||
@@ -729,7 +741,16 @@ class Interpreter(Script):
|
||||
super().__init__(code, environment=environment, project=project, **kwds)
|
||||
|
||||
self.namespaces = namespaces
|
||||
self._inference_state.allow_descriptor_getattr = self._allow_descriptor_getattr_default
|
||||
self._inference_state.allow_unsafe_executions = \
|
||||
settings.allow_unsafe_interpreter_executions
|
||||
# Dynamic params search is important when we work on functions that are
|
||||
# called by other pieces of code. However for interpreter completions
|
||||
# this is not important at all, because the current code is always new
|
||||
# and will never be called by something.
|
||||
# Also sometimes this logic goes a bit too far like in
|
||||
# https://github.com/ipython/ipython/issues/13866, where it takes
|
||||
# seconds to do a simple completion.
|
||||
self._inference_state.do_dynamic_params_search = False
|
||||
|
||||
@cache.memoize_method
|
||||
def _get_module_context(self):
|
||||
|
||||
@@ -105,7 +105,6 @@ class BaseName:
|
||||
# Compiled modules should not return a module path even if they
|
||||
# have one.
|
||||
path: Optional[Path] = self._get_module_context().py__file__()
|
||||
if path is not None:
|
||||
return path
|
||||
|
||||
return None
|
||||
|
||||
@@ -65,12 +65,15 @@ def _must_be_kwarg(signatures, positional_count, used_kwargs):
|
||||
return must_be_kwarg
|
||||
|
||||
|
||||
def filter_names(inference_state, completion_names, stack, like_name, fuzzy, cached_name):
|
||||
def filter_names(inference_state, completion_names, stack, like_name, fuzzy,
|
||||
imported_names, cached_name):
|
||||
comp_dct = set()
|
||||
if settings.case_insensitive_completion:
|
||||
like_name = like_name.lower()
|
||||
for name in completion_names:
|
||||
string = name.string_name
|
||||
if string in imported_names and string != like_name:
|
||||
continue
|
||||
if settings.case_insensitive_completion:
|
||||
string = string.lower()
|
||||
if helpers.match(string, like_name, fuzzy=fuzzy):
|
||||
@@ -138,6 +141,11 @@ class Completion:
|
||||
|
||||
self._fuzzy = fuzzy
|
||||
|
||||
# Return list of completions in this order:
|
||||
# - Beginning with what user is typing
|
||||
# - Public (alphabet)
|
||||
# - Private ("_xxx")
|
||||
# - Dunder ("__xxx")
|
||||
def complete(self):
|
||||
leaf = self._module_node.get_leaf_for_position(
|
||||
self._original_position,
|
||||
@@ -169,14 +177,19 @@ class Completion:
|
||||
|
||||
cached_name, completion_names = self._complete_python(leaf)
|
||||
|
||||
imported_names = []
|
||||
if leaf.parent is not None and leaf.parent.type in ['import_as_names', 'dotted_as_names']:
|
||||
imported_names.extend(extract_imported_names(leaf.parent))
|
||||
|
||||
completions = list(filter_names(self._inference_state, completion_names,
|
||||
self.stack, self._like_name,
|
||||
self._fuzzy, cached_name=cached_name))
|
||||
self._fuzzy, imported_names, cached_name=cached_name))
|
||||
|
||||
return (
|
||||
# Removing duplicates mostly to remove False/True/None duplicates.
|
||||
_remove_duplicates(prefixed_completions, completions)
|
||||
+ sorted(completions, key=lambda x: (x.name.startswith('__'),
|
||||
+ sorted(completions, key=lambda x: (not x.name.startswith(self._like_name),
|
||||
x.name.startswith('__'),
|
||||
x.name.startswith('_'),
|
||||
x.name.lower()))
|
||||
)
|
||||
@@ -442,6 +455,7 @@ class Completion:
|
||||
- Having some doctest code that starts with `>>>`
|
||||
- Having backticks that doesn't have whitespace inside it
|
||||
"""
|
||||
|
||||
def iter_relevant_lines(lines):
|
||||
include_next_line = False
|
||||
for l in code_lines:
|
||||
@@ -664,3 +678,19 @@ def search_in_module(inference_state, module_context, names, wanted_names,
|
||||
def_ = classes.Name(inference_state, n2)
|
||||
if not wanted_type or wanted_type == def_.type:
|
||||
yield def_
|
||||
|
||||
|
||||
def extract_imported_names(node):
|
||||
imported_names = []
|
||||
|
||||
if node.type in ['import_as_names', 'dotted_as_names', 'import_as_name']:
|
||||
for index, child in enumerate(node.children):
|
||||
if child.type == 'name':
|
||||
if (index > 0 and node.children[index - 1].type == "keyword"
|
||||
and node.children[index - 1].value == "as"):
|
||||
continue
|
||||
imported_names.append(child.value)
|
||||
elif child.type == 'import_as_name':
|
||||
imported_names.extend(extract_imported_names(child))
|
||||
|
||||
return imported_names
|
||||
|
||||
@@ -8,6 +8,7 @@ import hashlib
|
||||
import filecmp
|
||||
from collections import namedtuple
|
||||
from shutil import which
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from jedi.cache import memoize_method, time_cache
|
||||
from jedi.inference.compiled.subprocess import CompiledSubprocess, \
|
||||
@@ -15,9 +16,13 @@ from jedi.inference.compiled.subprocess import CompiledSubprocess, \
|
||||
|
||||
import parso
|
||||
|
||||
_VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
if TYPE_CHECKING:
|
||||
from jedi.inference import InferenceState
|
||||
|
||||
_SUPPORTED_PYTHONS = ['3.10', '3.9', '3.8', '3.7', '3.6']
|
||||
|
||||
_VersionInfo = namedtuple('VersionInfo', 'major minor micro') # type: ignore[name-match]
|
||||
|
||||
_SUPPORTED_PYTHONS = ['3.13', '3.12', '3.11', '3.10', '3.9', '3.8', '3.7', '3.6']
|
||||
_SAFE_PATHS = ['/usr/bin', '/usr/local/bin']
|
||||
_CONDA_VAR = 'CONDA_PREFIX'
|
||||
_CURRENT_VERSION = '%s.%s' % (sys.version_info.major, sys.version_info.minor)
|
||||
@@ -102,7 +107,10 @@ class Environment(_BaseEnvironment):
|
||||
version = '.'.join(str(i) for i in self.version_info)
|
||||
return '<%s: %s in %s>' % (self.__class__.__name__, version, self.path)
|
||||
|
||||
def get_inference_state_subprocess(self, inference_state):
|
||||
def get_inference_state_subprocess(
|
||||
self,
|
||||
inference_state: 'InferenceState',
|
||||
) -> InferenceStateSubprocess:
|
||||
return InferenceStateSubprocess(inference_state, self._get_subprocess())
|
||||
|
||||
@memoize_method
|
||||
@@ -134,7 +142,10 @@ class SameEnvironment(_SameEnvironmentMixin, Environment):
|
||||
|
||||
|
||||
class InterpreterEnvironment(_SameEnvironmentMixin, _BaseEnvironment):
|
||||
def get_inference_state_subprocess(self, inference_state):
|
||||
def get_inference_state_subprocess(
|
||||
self,
|
||||
inference_state: 'InferenceState',
|
||||
) -> InferenceStateSameProcess:
|
||||
return InferenceStateSameProcess(inference_state)
|
||||
|
||||
def get_sys_path(self):
|
||||
@@ -373,10 +384,13 @@ def _get_executable_path(path, safe=True):
|
||||
"""
|
||||
|
||||
if os.name == 'nt':
|
||||
python = os.path.join(path, 'Scripts', 'python.exe')
|
||||
pythons = [os.path.join(path, 'Scripts', 'python.exe'), os.path.join(path, 'python.exe')]
|
||||
else:
|
||||
pythons = [os.path.join(path, 'bin', 'python')]
|
||||
for python in pythons:
|
||||
if os.path.exists(python):
|
||||
break
|
||||
else:
|
||||
python = os.path.join(path, 'bin', 'python')
|
||||
if not os.path.exists(python):
|
||||
raise InvalidPythonEnvironment("%s seems to be missing." % python)
|
||||
|
||||
_assert_safe(python, safe)
|
||||
@@ -384,8 +398,7 @@ def _get_executable_path(path, safe=True):
|
||||
|
||||
|
||||
def _get_executables_from_windows_registry(version):
|
||||
# https://github.com/python/typeshed/pull/3794 adds winreg
|
||||
import winreg # type: ignore[import]
|
||||
import winreg
|
||||
|
||||
# TODO: support Python Anaconda.
|
||||
sub_keys = [
|
||||
|
||||
@@ -5,8 +5,7 @@ from typing import Dict, Optional
|
||||
from jedi.inference.names import AbstractArbitraryName
|
||||
|
||||
try:
|
||||
# https://github.com/python/typeshed/pull/4351 adds pydoc_data
|
||||
from pydoc_data import topics # type: ignore[import]
|
||||
from pydoc_data import topics
|
||||
pydoc_topics: Optional[Dict[str, str]] = topics.topics
|
||||
except ImportError:
|
||||
# Python 3.6.8 embeddable does not have pydoc_data.
|
||||
|
||||
@@ -5,6 +5,7 @@ from typing import Dict, Iterable, Tuple
|
||||
from parso import split_lines
|
||||
|
||||
from jedi.api.exceptions import RefactoringError
|
||||
from jedi.inference.value.namespace import ImplicitNSName
|
||||
|
||||
EXPRESSION_PARTS = (
|
||||
'or_test and_test not_test comparison '
|
||||
@@ -102,7 +103,12 @@ class Refactoring:
|
||||
to_path=calculate_to_path(path),
|
||||
module_node=next(iter(map_)).get_root_node(),
|
||||
node_to_str_map=map_
|
||||
) for path, map_ in sorted(self._file_to_node_changes.items())
|
||||
)
|
||||
# We need to use `or`, because the path can be None
|
||||
for path, map_ in sorted(
|
||||
self._file_to_node_changes.items(),
|
||||
key=lambda x: x[0] or Path("")
|
||||
)
|
||||
}
|
||||
|
||||
def get_renames(self) -> Iterable[Tuple[Path, Path]]:
|
||||
@@ -116,7 +122,7 @@ class Refactoring:
|
||||
project_path = self._inference_state.project.path
|
||||
for from_, to in self.get_renames():
|
||||
text += 'rename from %s\nrename to %s\n' \
|
||||
% (from_.relative_to(project_path), to.relative_to(project_path))
|
||||
% (_try_relative_to(from_, project_path), _try_relative_to(to, project_path))
|
||||
|
||||
return text + ''.join(f.get_diff() for f in self.get_changed_files().values())
|
||||
|
||||
@@ -146,13 +152,16 @@ def rename(inference_state, definitions, new_name):
|
||||
raise RefactoringError("There is no name under the cursor")
|
||||
|
||||
for d in definitions:
|
||||
tree_name = d._name.tree_name
|
||||
if d.type == 'module' and tree_name is None:
|
||||
p = None if d.module_path is None else Path(d.module_path)
|
||||
file_renames.add(_calculate_rename(p, new_name))
|
||||
else:
|
||||
# This private access is ok in a way. It's not public to
|
||||
# protect Jedi users from seeing it.
|
||||
tree_name = d._name.tree_name
|
||||
if d.type == 'module' and tree_name is None and d.module_path is not None:
|
||||
p = Path(d.module_path)
|
||||
file_renames.add(_calculate_rename(p, new_name))
|
||||
elif isinstance(d._name, ImplicitNSName):
|
||||
for p in d._name._value.py__path__():
|
||||
file_renames.add(_calculate_rename(Path(p), new_name))
|
||||
else:
|
||||
if tree_name is not None:
|
||||
fmap = file_tree_name_map.setdefault(d.module_path, {})
|
||||
fmap[tree_name] = tree_name.prefix + new_name
|
||||
@@ -246,3 +255,10 @@ def _remove_indent_of_prefix(prefix):
|
||||
Removes the last indentation of a prefix, e.g. " \n \n " becomes " \n \n".
|
||||
"""
|
||||
return ''.join(split_lines(prefix, keepends=True)[:-1])
|
||||
|
||||
|
||||
def _try_relative_to(path: Path, base: Path) -> Path:
|
||||
try:
|
||||
return path.relative_to(base)
|
||||
except ValueError:
|
||||
return path
|
||||
|
||||
@@ -36,8 +36,11 @@ def complete_dict(module_context, code_lines, leaf, position, string, fuzzy):
|
||||
string = cut_value_at_position(leaf, position)
|
||||
|
||||
context = module_context.create_context(bracket_leaf)
|
||||
before_bracket_leaf = bracket_leaf.get_previous_leaf()
|
||||
if before_bracket_leaf.type in ('atom', 'trailer', 'name'):
|
||||
|
||||
before_node = before_bracket_leaf = bracket_leaf.get_previous_leaf()
|
||||
if before_node in (')', ']', '}'):
|
||||
before_node = before_node.parent
|
||||
if before_node.type in ('atom', 'trailer', 'name'):
|
||||
values = infer_call_of_leaf(context, before_bracket_leaf)
|
||||
return list(_completions_for_dicts(
|
||||
module_context.inference_state,
|
||||
|
||||
@@ -90,7 +90,7 @@ class InferenceState:
|
||||
self.compiled_subprocess = environment.get_inference_state_subprocess(self)
|
||||
self.grammar = environment.get_grammar()
|
||||
|
||||
self.latest_grammar = parso.load_grammar(version='3.10')
|
||||
self.latest_grammar = parso.load_grammar(version='3.13')
|
||||
self.memoize_cache = {} # for memoize decorators
|
||||
self.module_cache = imports.ModuleCache() # does the job of `sys.modules`.
|
||||
self.stub_module_cache = {} # Dict[Tuple[str, ...], Optional[ModuleValue]]
|
||||
@@ -99,10 +99,11 @@ class InferenceState:
|
||||
self.mixed_cache = {} # see `inference.compiled.mixed._create()`
|
||||
self.analysis = []
|
||||
self.dynamic_params_depth = 0
|
||||
self.do_dynamic_params_search = settings.dynamic_params
|
||||
self.is_analysis = False
|
||||
self.project = project
|
||||
self.access_cache = {}
|
||||
self.allow_descriptor_getattr = False
|
||||
self.allow_unsafe_executions = False
|
||||
self.flow_analysis_enabled = True
|
||||
|
||||
self.reset_recursion_limitations()
|
||||
@@ -125,7 +126,7 @@ class InferenceState:
|
||||
@inference_state_function_cache()
|
||||
def builtins_module(self):
|
||||
module_name = 'builtins'
|
||||
builtins_module, = self.import_module((module_name,), sys_path=())
|
||||
builtins_module, = self.import_module((module_name,), sys_path=[])
|
||||
return builtins_module
|
||||
|
||||
@property # type: ignore[misc]
|
||||
|
||||
@@ -9,7 +9,7 @@ import re
|
||||
import builtins
|
||||
import typing
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from typing import Optional, Tuple
|
||||
|
||||
from jedi.inference.compiled.getattr_static import getattr_static
|
||||
|
||||
@@ -40,7 +40,7 @@ NOT_CLASS_TYPES = (
|
||||
MethodDescriptorType = type(str.replace)
|
||||
WrapperDescriptorType = type(set.__iter__)
|
||||
# `object.__subclasshook__` is an already executed descriptor.
|
||||
object_class_dict = type.__dict__["__dict__"].__get__(object)
|
||||
object_class_dict = type.__dict__["__dict__"].__get__(object) # type: ignore[index]
|
||||
ClassMethodDescriptorType = type(object_class_dict['__subclasshook__'])
|
||||
|
||||
_sentinel = object()
|
||||
@@ -147,7 +147,7 @@ class AccessPath:
|
||||
self.accesses = accesses
|
||||
|
||||
|
||||
def create_access_path(inference_state, obj):
|
||||
def create_access_path(inference_state, obj) -> AccessPath:
|
||||
access = create_access(inference_state, obj)
|
||||
return AccessPath(access.get_access_path_tuples())
|
||||
|
||||
@@ -175,7 +175,7 @@ class DirectObjectAccess:
|
||||
def _create_access(self, obj):
|
||||
return create_access(self._inference_state, obj)
|
||||
|
||||
def _create_access_path(self, obj):
|
||||
def _create_access_path(self, obj) -> AccessPath:
|
||||
return create_access_path(self._inference_state, obj)
|
||||
|
||||
def py__bool__(self):
|
||||
@@ -230,8 +230,8 @@ class DirectObjectAccess:
|
||||
return [annotation]
|
||||
return None
|
||||
|
||||
def py__simple_getitem__(self, index):
|
||||
if type(self._obj) not in ALLOWED_GETITEM_TYPES:
|
||||
def py__simple_getitem__(self, index, *, safe=True):
|
||||
if safe and type(self._obj) not in ALLOWED_GETITEM_TYPES:
|
||||
# Get rid of side effects, we won't call custom `__getitem__`s.
|
||||
return None
|
||||
|
||||
@@ -329,8 +329,11 @@ class DirectObjectAccess:
|
||||
except TypeError:
|
||||
return False
|
||||
|
||||
def is_allowed_getattr(self, name, safe=True):
|
||||
def is_allowed_getattr(self, name, safe=True) -> Tuple[bool, bool, Optional[AccessPath]]:
|
||||
# TODO this API is ugly.
|
||||
try:
|
||||
attr, is_get_descriptor = getattr_static(self._obj, name)
|
||||
except AttributeError:
|
||||
if not safe:
|
||||
# Unsafe is mostly used to check for __getattr__/__getattribute__.
|
||||
# getattr_static works for properties, but the underscore methods
|
||||
@@ -341,21 +344,22 @@ class DirectObjectAccess:
|
||||
with warnings.catch_warnings(record=True):
|
||||
warnings.simplefilter("always")
|
||||
try:
|
||||
return hasattr(self._obj, name), False
|
||||
return hasattr(self._obj, name), False, None
|
||||
except Exception:
|
||||
# Obviously has an attribute (propably a property) that
|
||||
# Obviously has an attribute (probably a property) that
|
||||
# gets executed, so just avoid all exceptions here.
|
||||
return False, False
|
||||
try:
|
||||
attr, is_get_descriptor = getattr_static(self._obj, name)
|
||||
except AttributeError:
|
||||
return False, False
|
||||
pass
|
||||
return False, False, None
|
||||
else:
|
||||
if is_get_descriptor and type(attr) not in ALLOWED_DESCRIPTOR_ACCESS:
|
||||
if isinstance(attr, property):
|
||||
if hasattr(attr.fget, '__annotations__'):
|
||||
a = DirectObjectAccess(self._inference_state, attr.fget)
|
||||
return True, True, a.get_return_annotation()
|
||||
# In case of descriptors that have get methods we cannot return
|
||||
# it's value, because that would mean code execution.
|
||||
return True, True
|
||||
return True, False
|
||||
return True, True, None
|
||||
return True, False, None
|
||||
|
||||
def getattr_paths(self, name, default=_sentinel):
|
||||
try:
|
||||
@@ -515,7 +519,7 @@ class DirectObjectAccess:
|
||||
# the signature. In that case we just want a simple escape for now.
|
||||
raise ValueError
|
||||
|
||||
def get_return_annotation(self):
|
||||
def get_return_annotation(self) -> Optional[AccessPath]:
|
||||
try:
|
||||
o = self._obj.__annotations__.get('return')
|
||||
except AttributeError:
|
||||
|
||||
@@ -142,9 +142,9 @@ class MixedObjectFilter(compiled.CompiledValueFilter):
|
||||
super().__init__(inference_state, compiled_value)
|
||||
self._tree_value = tree_value
|
||||
|
||||
def _create_name(self, name):
|
||||
def _create_name(self, *args, **kwargs):
|
||||
return MixedName(
|
||||
super()._create_name(name),
|
||||
super()._create_name(*args, **kwargs),
|
||||
self._tree_value,
|
||||
)
|
||||
|
||||
|
||||
@@ -5,6 +5,23 @@ goals:
|
||||
1. Making it safer - Segfaults and RuntimeErrors as well as stdout/stderr can
|
||||
be ignored and dealt with.
|
||||
2. Make it possible to handle different Python versions as well as virtualenvs.
|
||||
|
||||
The architecture here is briefly:
|
||||
- For each Jedi `Environment` there is a corresponding subprocess which
|
||||
operates within the target environment. If the subprocess dies it is replaced
|
||||
at this level.
|
||||
- `CompiledSubprocess` manages exactly one subprocess and handles communication
|
||||
from the parent side.
|
||||
- `Listener` runs within the subprocess, processing each request and yielding
|
||||
results.
|
||||
- `InterpreterEnvironment` provides an API which matches that of `Environment`,
|
||||
but runs functionality inline rather than within a subprocess. It is thus
|
||||
used both directly in places where a subprocess is unnecessary and/or
|
||||
undesirable and also within subprocesses themselves.
|
||||
- `InferenceStateSubprocess` (or `InferenceStateSameProcess`) provide high
|
||||
level access to functionality within the subprocess from within the parent.
|
||||
Each `InterpreterState` has an instance of one of these, provided by its
|
||||
environment.
|
||||
"""
|
||||
|
||||
import collections
|
||||
@@ -16,6 +33,7 @@ import traceback
|
||||
import weakref
|
||||
from functools import partial
|
||||
from threading import Thread
|
||||
from typing import Dict, TYPE_CHECKING
|
||||
|
||||
from jedi._compatibility import pickle_dump, pickle_load
|
||||
from jedi import debug
|
||||
@@ -25,6 +43,9 @@ from jedi.inference.compiled.access import DirectObjectAccess, AccessPath, \
|
||||
SignatureParam
|
||||
from jedi.api.exceptions import InternalError
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from jedi.inference import InferenceState
|
||||
|
||||
|
||||
_MAIN_PATH = os.path.join(os.path.dirname(__file__), '__main__.py')
|
||||
PICKLE_PROTOCOL = 4
|
||||
@@ -83,10 +104,9 @@ def _cleanup_process(process, thread):
|
||||
|
||||
|
||||
class _InferenceStateProcess:
|
||||
def __init__(self, inference_state):
|
||||
def __init__(self, inference_state: 'InferenceState') -> None:
|
||||
self._inference_state_weakref = weakref.ref(inference_state)
|
||||
self._inference_state_id = id(inference_state)
|
||||
self._handles = {}
|
||||
self._handles: Dict[int, AccessHandle] = {}
|
||||
|
||||
def get_or_create_access_handle(self, obj):
|
||||
id_ = id(obj)
|
||||
@@ -116,11 +136,49 @@ class InferenceStateSameProcess(_InferenceStateProcess):
|
||||
|
||||
|
||||
class InferenceStateSubprocess(_InferenceStateProcess):
|
||||
def __init__(self, inference_state, compiled_subprocess):
|
||||
"""
|
||||
API to functionality which will run in a subprocess.
|
||||
|
||||
This mediates the interaction between an `InferenceState` and the actual
|
||||
execution of functionality running within a `CompiledSubprocess`. Available
|
||||
functions are defined in `.functions`, though should be accessed via
|
||||
attributes on this class of the same name.
|
||||
|
||||
This class is responsible for indicating that the `InferenceState` within
|
||||
the subprocess can be removed once the corresponding instance in the parent
|
||||
goes away.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
inference_state: 'InferenceState',
|
||||
compiled_subprocess: 'CompiledSubprocess',
|
||||
) -> None:
|
||||
super().__init__(inference_state)
|
||||
self._used = False
|
||||
self._compiled_subprocess = compiled_subprocess
|
||||
|
||||
# Opaque id we'll pass to the subprocess to identify the context (an
|
||||
# `InferenceState`) which should be used for the request. This allows us
|
||||
# to make subsequent requests which operate on results from previous
|
||||
# ones, while keeping a single subprocess which can work with several
|
||||
# contexts in the parent process. Once it is no longer needed(i.e: when
|
||||
# this class goes away), we also use this id to indicate that the
|
||||
# subprocess can discard the context.
|
||||
#
|
||||
# Note: this id is deliberately coupled to this class (and not to
|
||||
# `InferenceState`) as this class manages access handle mappings which
|
||||
# must correspond to those in the subprocess. This approach also avoids
|
||||
# race conditions from successive `InferenceState`s with the same object
|
||||
# id (as observed while adding support for Python 3.13).
|
||||
#
|
||||
# This value does not need to be the `id()` of this instance, we merely
|
||||
# need to ensure that it enables the (visible) lifetime of the context
|
||||
# within the subprocess to match that of this class. We therefore also
|
||||
# depend on the semantics of `CompiledSubprocess.delete_inference_state`
|
||||
# for correctness.
|
||||
self._inference_state_id = id(self)
|
||||
|
||||
def __getattr__(self, name):
|
||||
func = _get_function(name)
|
||||
|
||||
@@ -128,7 +186,7 @@ class InferenceStateSubprocess(_InferenceStateProcess):
|
||||
self._used = True
|
||||
|
||||
result = self._compiled_subprocess.run(
|
||||
self._inference_state_weakref(),
|
||||
self._inference_state_id,
|
||||
func,
|
||||
args=args,
|
||||
kwargs=kwargs,
|
||||
@@ -164,6 +222,17 @@ class InferenceStateSubprocess(_InferenceStateProcess):
|
||||
|
||||
|
||||
class CompiledSubprocess:
|
||||
"""
|
||||
A subprocess which runs inference within a target environment.
|
||||
|
||||
This class manages the interface to a single instance of such a process as
|
||||
well as the lifecycle of the process itself. See `.__main__` and `Listener`
|
||||
for the implementation of the subprocess and details of the protocol.
|
||||
|
||||
A single live instance of this is maintained by `jedi.api.environment.Environment`,
|
||||
so that typically a single subprocess is used at a time.
|
||||
"""
|
||||
|
||||
is_crashed = False
|
||||
|
||||
def __init__(self, executable, env_vars=None):
|
||||
@@ -213,18 +282,18 @@ class CompiledSubprocess:
|
||||
t)
|
||||
return process
|
||||
|
||||
def run(self, inference_state, function, args=(), kwargs={}):
|
||||
def run(self, inference_state_id, function, args=(), kwargs={}):
|
||||
# Delete old inference_states.
|
||||
while True:
|
||||
try:
|
||||
inference_state_id = self._inference_state_deletion_queue.pop()
|
||||
delete_id = self._inference_state_deletion_queue.pop()
|
||||
except IndexError:
|
||||
break
|
||||
else:
|
||||
self._send(inference_state_id, None)
|
||||
self._send(delete_id, None)
|
||||
|
||||
assert callable(function)
|
||||
return self._send(id(inference_state), function, args, kwargs)
|
||||
return self._send(inference_state_id, function, args, kwargs)
|
||||
|
||||
def get_sys_path(self):
|
||||
return self._send(None, functions.get_sys_path, (), {})
|
||||
@@ -272,21 +341,65 @@ class CompiledSubprocess:
|
||||
|
||||
def delete_inference_state(self, inference_state_id):
|
||||
"""
|
||||
Currently we are not deleting inference_state instantly. They only get
|
||||
deleted once the subprocess is used again. It would probably a better
|
||||
solution to move all of this into a thread. However, the memory usage
|
||||
of a single inference_state shouldn't be that high.
|
||||
Indicate that an inference state (in the subprocess) is no longer
|
||||
needed.
|
||||
|
||||
The state corresponding to the given id will become inaccessible and the
|
||||
id may safely be re-used to refer to a different context.
|
||||
|
||||
Note: it is not guaranteed that the corresponding state will actually be
|
||||
deleted immediately.
|
||||
"""
|
||||
# With an argument - the inference_state gets deleted.
|
||||
# Warning: if changing the semantics of context deletion see the comment
|
||||
# in `InferenceStateSubprocess.__init__` regarding potential race
|
||||
# conditions.
|
||||
|
||||
# Currently we are not deleting the related state instantly. They only
|
||||
# get deleted once the subprocess is used again. It would probably a
|
||||
# better solution to move all of this into a thread. However, the memory
|
||||
# usage of a single inference_state shouldn't be that high.
|
||||
self._inference_state_deletion_queue.append(inference_state_id)
|
||||
|
||||
|
||||
class Listener:
|
||||
"""
|
||||
Main loop for the subprocess which actually does the inference.
|
||||
|
||||
This class runs within the target environment. It listens to instructions
|
||||
from the parent process, runs inference and returns the results.
|
||||
|
||||
The subprocess has a long lifetime and is expected to process several
|
||||
requests, including for different `InferenceState` instances in the parent.
|
||||
See `CompiledSubprocess` for the parent half of the system.
|
||||
|
||||
Communication is via pickled data sent serially over stdin and stdout.
|
||||
Stderr is read only if the child process crashes.
|
||||
|
||||
The request protocol is a 4-tuple of:
|
||||
* inference_state_id | None: an opaque identifier of the parent's
|
||||
`InferenceState`. An `InferenceState` operating over an
|
||||
`InterpreterEnvironment` is created within this process for each of
|
||||
these, ensuring that each parent context has a corresponding context
|
||||
here. This allows context to be persisted between requests. Unless
|
||||
`None`, the local `InferenceState` will be passed to the given function
|
||||
as the first positional argument.
|
||||
* function | None: the function to run. This is expected to be a member of
|
||||
`.functions`. `None` indicates that the corresponding inference state is
|
||||
no longer needed and should be dropped.
|
||||
* args: positional arguments to the `function`. If any of these are
|
||||
`AccessHandle` instances they will be adapted to the local
|
||||
`InferenceState` before being passed.
|
||||
* kwargs: keyword arguments to the `function`. If any of these are
|
||||
`AccessHandle` instances they will be adapted to the local
|
||||
`InferenceState` before being passed.
|
||||
|
||||
The result protocol is a 3-tuple of either:
|
||||
* (False, None, function result): if the function returns without error, or
|
||||
* (True, traceback, exception): if the function raises an exception
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._inference_states = {}
|
||||
# TODO refactor so we don't need to process anymore just handle
|
||||
# controlling.
|
||||
self._process = _InferenceStateProcess(Listener)
|
||||
|
||||
def _get_inference_state(self, function, inference_state_id):
|
||||
from jedi.inference import InferenceState
|
||||
@@ -308,6 +421,9 @@ class Listener:
|
||||
if inference_state_id is None:
|
||||
return function(*args, **kwargs)
|
||||
elif function is None:
|
||||
# Warning: if changing the semantics of context deletion see the comment
|
||||
# in `InferenceStateSubprocess.__init__` regarding potential race
|
||||
# conditions.
|
||||
del self._inference_states[inference_state_id]
|
||||
else:
|
||||
inference_state = self._get_inference_state(function, inference_state_id)
|
||||
@@ -348,7 +464,12 @@ class Listener:
|
||||
|
||||
|
||||
class AccessHandle:
|
||||
def __init__(self, subprocess, access, id_):
|
||||
def __init__(
|
||||
self,
|
||||
subprocess: _InferenceStateProcess,
|
||||
access: DirectObjectAccess,
|
||||
id_: int,
|
||||
) -> None:
|
||||
self.access = access
|
||||
self._subprocess = subprocess
|
||||
self.id = id_
|
||||
|
||||
@@ -21,11 +21,11 @@ class _ExactImporter(MetaPathFinder):
|
||||
def __init__(self, path_dct):
|
||||
self._path_dct = path_dct
|
||||
|
||||
def find_module(self, fullname, path=None):
|
||||
def find_spec(self, fullname, path=None, target=None):
|
||||
if path is None and fullname in self._path_dct:
|
||||
p = self._path_dct[fullname]
|
||||
loader = PathFinder.find_module(fullname, path=[p])
|
||||
return loader
|
||||
spec = PathFinder.find_spec(fullname, path=[p], target=target)
|
||||
return spec
|
||||
return None
|
||||
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ import sys
|
||||
import os
|
||||
import inspect
|
||||
import importlib
|
||||
import warnings
|
||||
from pathlib import Path
|
||||
from zipfile import ZipFile
|
||||
from zipimport import zipimporter, ZipImportError
|
||||
@@ -167,17 +166,16 @@ def _find_module(string, path=None, full_name=None, is_global_search=True):
|
||||
|
||||
|
||||
def _find_module_py33(string, path=None, loader=None, full_name=None, is_global_search=True):
|
||||
loader = loader or importlib.machinery.PathFinder.find_module(string, path)
|
||||
if not loader:
|
||||
spec = importlib.machinery.PathFinder.find_spec(string, path)
|
||||
if spec is not None:
|
||||
loader = spec.loader
|
||||
|
||||
if loader is None and path is None: # Fallback to find builtins
|
||||
try:
|
||||
with warnings.catch_warnings(record=True):
|
||||
# Mute "DeprecationWarning: Use importlib.util.find_spec()
|
||||
# instead." While we should replace that in the future, it's
|
||||
# probably good to wait until we deprecate Python 3.3, since
|
||||
# it was added in Python 3.4 and find_loader hasn't been
|
||||
# removed in 3.6.
|
||||
loader = importlib.find_loader(string)
|
||||
spec = importlib.util.find_spec(string)
|
||||
if spec is not None:
|
||||
loader = spec.loader
|
||||
except ValueError as e:
|
||||
# See #491. Importlib might raise a ValueError, to avoid this, we
|
||||
# just raise an ImportError to fix the issue.
|
||||
|
||||
@@ -51,7 +51,6 @@ class CompiledValue(Value):
|
||||
def py__call__(self, arguments):
|
||||
return_annotation = self.access_handle.get_return_annotation()
|
||||
if return_annotation is not None:
|
||||
# TODO the return annotation may also be a string.
|
||||
return create_from_access_path(
|
||||
self.inference_state,
|
||||
return_annotation
|
||||
@@ -163,7 +162,10 @@ class CompiledValue(Value):
|
||||
def py__simple_getitem__(self, index):
|
||||
with reraise_getitem_errors(IndexError, KeyError, TypeError):
|
||||
try:
|
||||
access = self.access_handle.py__simple_getitem__(index)
|
||||
access = self.access_handle.py__simple_getitem__(
|
||||
index,
|
||||
safe=not self.inference_state.allow_unsafe_executions
|
||||
)
|
||||
except AttributeError:
|
||||
return super().py__simple_getitem__(index)
|
||||
if access is None:
|
||||
@@ -311,11 +313,12 @@ class CompiledModule(CompiledValue):
|
||||
|
||||
|
||||
class CompiledName(AbstractNameDefinition):
|
||||
def __init__(self, inference_state, parent_value, name):
|
||||
def __init__(self, inference_state, parent_value, name, is_descriptor):
|
||||
self._inference_state = inference_state
|
||||
self.parent_context = parent_value.as_context()
|
||||
self._parent_value = parent_value
|
||||
self.string_name = name
|
||||
self.is_descriptor = is_descriptor
|
||||
|
||||
def py__doc__(self):
|
||||
return self.infer_compiled_value().py__doc__()
|
||||
@@ -342,6 +345,11 @@ class CompiledName(AbstractNameDefinition):
|
||||
|
||||
@property
|
||||
def api_type(self):
|
||||
if self.is_descriptor:
|
||||
# In case of properties we want to avoid executions as much as
|
||||
# possible. Since the api_type can be wrong for other reasons
|
||||
# anyway, we just return instance here.
|
||||
return "instance"
|
||||
return self.infer_compiled_value().api_type
|
||||
|
||||
def infer(self):
|
||||
@@ -432,9 +440,10 @@ class CompiledValueFilter(AbstractFilter):
|
||||
|
||||
def get(self, name):
|
||||
access_handle = self.compiled_value.access_handle
|
||||
safe = not self._inference_state.allow_unsafe_executions
|
||||
return self._get(
|
||||
name,
|
||||
lambda name, safe: access_handle.is_allowed_getattr(name, safe=safe),
|
||||
lambda name: access_handle.is_allowed_getattr(name, safe=safe),
|
||||
lambda name: name in access_handle.dir(),
|
||||
check_has_attribute=True
|
||||
)
|
||||
@@ -443,30 +452,34 @@ class CompiledValueFilter(AbstractFilter):
|
||||
"""
|
||||
To remove quite a few access calls we introduced the callback here.
|
||||
"""
|
||||
if self._inference_state.allow_descriptor_getattr:
|
||||
pass
|
||||
|
||||
has_attribute, is_descriptor = allowed_getattr_callback(
|
||||
has_attribute, is_descriptor, property_return_annotation = allowed_getattr_callback(
|
||||
name,
|
||||
safe=not self._inference_state.allow_descriptor_getattr
|
||||
)
|
||||
if property_return_annotation is not None:
|
||||
values = create_from_access_path(
|
||||
self._inference_state,
|
||||
property_return_annotation
|
||||
).execute_annotation()
|
||||
if values:
|
||||
return [CompiledValueName(v, name) for v in values]
|
||||
|
||||
if check_has_attribute and not has_attribute:
|
||||
return []
|
||||
|
||||
if (is_descriptor or not has_attribute) \
|
||||
and not self._inference_state.allow_descriptor_getattr:
|
||||
and not self._inference_state.allow_unsafe_executions:
|
||||
return [self._get_cached_name(name, is_empty=True)]
|
||||
|
||||
if self.is_instance and not in_dir_callback(name):
|
||||
return []
|
||||
return [self._get_cached_name(name)]
|
||||
return [self._get_cached_name(name, is_descriptor=is_descriptor)]
|
||||
|
||||
@memoize_method
|
||||
def _get_cached_name(self, name, is_empty=False):
|
||||
def _get_cached_name(self, name, is_empty=False, *, is_descriptor=False):
|
||||
if is_empty:
|
||||
return EmptyCompiledName(self._inference_state, name)
|
||||
else:
|
||||
return self._create_name(name)
|
||||
return self._create_name(name, is_descriptor=is_descriptor)
|
||||
|
||||
def values(self):
|
||||
from jedi.inference.compiled import builtin_from_name
|
||||
@@ -480,7 +493,7 @@ class CompiledValueFilter(AbstractFilter):
|
||||
for name in dir_infos:
|
||||
names += self._get(
|
||||
name,
|
||||
lambda name, safe: dir_infos[name],
|
||||
lambda name: dir_infos[name],
|
||||
lambda name: name in dir_infos,
|
||||
)
|
||||
|
||||
@@ -490,11 +503,12 @@ class CompiledValueFilter(AbstractFilter):
|
||||
names += filter.values()
|
||||
return names
|
||||
|
||||
def _create_name(self, name):
|
||||
def _create_name(self, name, is_descriptor):
|
||||
return CompiledName(
|
||||
self._inference_state,
|
||||
self.compiled_value,
|
||||
name
|
||||
name,
|
||||
is_descriptor,
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
|
||||
@@ -66,11 +66,11 @@ def dynamic_param_lookup(function_value, param_index):
|
||||
have to look for all calls to ``func`` to find out what ``foo`` possibly
|
||||
is.
|
||||
"""
|
||||
funcdef = function_value.tree_node
|
||||
|
||||
if not settings.dynamic_params:
|
||||
if not function_value.inference_state.do_dynamic_params_search:
|
||||
return NO_VALUES
|
||||
|
||||
funcdef = function_value.tree_node
|
||||
|
||||
path = function_value.get_root_context().py__file__()
|
||||
if path is not None and is_stdlib_path(path):
|
||||
# We don't want to search for references in the stdlib. Usually people
|
||||
|
||||
@@ -402,6 +402,10 @@ def find_type_from_comment_hint_for(context, node, name):
|
||||
|
||||
|
||||
def find_type_from_comment_hint_with(context, node, name):
|
||||
if len(node.children) > 4:
|
||||
# In case there are multiple with_items, we do not want a type hint for
|
||||
# now.
|
||||
return []
|
||||
assert len(node.children[1].children) == 3, \
|
||||
"Can only be here when children[1] is 'foo() as f'"
|
||||
varlist = node.children[1].children[2]
|
||||
|
||||
@@ -32,7 +32,7 @@ _TYPE_ALIAS_TYPES = {
|
||||
'DefaultDict': 'collections.defaultdict',
|
||||
'Deque': 'collections.deque',
|
||||
}
|
||||
_PROXY_TYPES = 'Optional Union ClassVar'.split()
|
||||
_PROXY_TYPES = 'Optional Union ClassVar Annotated'.split()
|
||||
|
||||
|
||||
class TypingModuleName(NameWrapper):
|
||||
@@ -113,7 +113,7 @@ class ProxyWithGenerics(BaseTypingClassWithGenerics):
|
||||
elif string_name == 'Type':
|
||||
# The type is actually already given in the index_value
|
||||
return self._generics_manager[0]
|
||||
elif string_name == 'ClassVar':
|
||||
elif string_name in ['ClassVar', 'Annotated']:
|
||||
# For now don't do anything here, ClassVars are always used.
|
||||
return self._generics_manager[0].execute_annotation()
|
||||
|
||||
|
||||
@@ -493,7 +493,9 @@ def infer_factor(value_set, operator):
|
||||
elif operator == 'not':
|
||||
b = value.py__bool__()
|
||||
if b is None: # Uncertainty.
|
||||
return
|
||||
yield list(value.inference_state.builtins_module.py__getattribute__('bool')
|
||||
.execute_annotation()).pop()
|
||||
else:
|
||||
yield compiled.create_simple_object(value.inference_state, not b)
|
||||
else:
|
||||
yield value
|
||||
@@ -645,7 +647,7 @@ def _infer_comparison_part(inference_state, context, left, operator, right):
|
||||
_bool_to_value(inference_state, False)
|
||||
])
|
||||
elif str_operator in ('in', 'not in'):
|
||||
return NO_VALUES
|
||||
return inference_state.builtins_module.py__getattribute__('bool').execute_annotation()
|
||||
|
||||
def check(obj):
|
||||
"""Checks if a Jedi object is either a float or an int."""
|
||||
@@ -695,8 +697,15 @@ def tree_name_to_values(inference_state, context, tree_name):
|
||||
|
||||
if expr_stmt.type == "expr_stmt" and expr_stmt.children[1].type == "annassign":
|
||||
correct_scope = parser_utils.get_parent_scope(name) == context.tree_node
|
||||
ann_assign = expr_stmt.children[1]
|
||||
if correct_scope:
|
||||
found_annotation = True
|
||||
if (
|
||||
(ann_assign.children[1].type == 'name')
|
||||
and (ann_assign.children[1].value == tree_name.value)
|
||||
and context.parent_context
|
||||
):
|
||||
context = context.parent_context
|
||||
value_set |= annotation.infer_annotation(
|
||||
context, expr_stmt.children[1].children[1]
|
||||
).execute_annotation()
|
||||
|
||||
@@ -320,7 +320,7 @@ def expr_is_dotted(node):
|
||||
return node.type == 'name'
|
||||
|
||||
|
||||
def _function_is_x_method(*method_names):
|
||||
def _function_is_x_method(decorator_checker):
|
||||
def wrapper(function_node):
|
||||
"""
|
||||
This is a heuristic. It will not hold ALL the times, but it will be
|
||||
@@ -330,12 +330,16 @@ def _function_is_x_method(*method_names):
|
||||
"""
|
||||
for decorator in function_node.get_decorators():
|
||||
dotted_name = decorator.children[1]
|
||||
if dotted_name.get_code() in method_names:
|
||||
if decorator_checker(dotted_name.get_code()):
|
||||
return True
|
||||
return False
|
||||
return wrapper
|
||||
|
||||
|
||||
function_is_staticmethod = _function_is_x_method('staticmethod')
|
||||
function_is_classmethod = _function_is_x_method('classmethod')
|
||||
function_is_property = _function_is_x_method('property', 'cached_property')
|
||||
function_is_staticmethod = _function_is_x_method(lambda m: m == "staticmethod")
|
||||
function_is_classmethod = _function_is_x_method(lambda m: m == "classmethod")
|
||||
function_is_property = _function_is_x_method(
|
||||
lambda m: m == "property"
|
||||
or m == "cached_property"
|
||||
or (m.endswith(".setter"))
|
||||
)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import sys
|
||||
from typing import List
|
||||
from pathlib import Path
|
||||
|
||||
from parso.tree import search_ancestor
|
||||
@@ -131,14 +133,33 @@ def _is_pytest_func(func_name, decorator_nodes):
|
||||
or any('fixture' in n.get_code() for n in decorator_nodes)
|
||||
|
||||
|
||||
def _find_pytest_plugin_modules():
|
||||
def _find_pytest_plugin_modules() -> List[List[str]]:
|
||||
"""
|
||||
Finds pytest plugin modules hooked by setuptools entry points
|
||||
|
||||
See https://docs.pytest.org/en/stable/how-to/writing_plugins.html#setuptools-entry-points
|
||||
"""
|
||||
from pkg_resources import iter_entry_points
|
||||
if sys.version_info >= (3, 8):
|
||||
from importlib.metadata import entry_points
|
||||
|
||||
if sys.version_info >= (3, 10):
|
||||
pytest_entry_points = entry_points(group="pytest11")
|
||||
else:
|
||||
pytest_entry_points = entry_points().get("pytest11", ())
|
||||
|
||||
if sys.version_info >= (3, 9):
|
||||
return [ep.module.split(".") for ep in pytest_entry_points]
|
||||
else:
|
||||
# Python 3.8 doesn't have `EntryPoint.module`. Implement equivalent
|
||||
# to what Python 3.9 does (with additional None check to placate `mypy`)
|
||||
matches = [
|
||||
ep.pattern.match(ep.value)
|
||||
for ep in pytest_entry_points
|
||||
]
|
||||
return [x.group('module').split(".") for x in matches if x]
|
||||
|
||||
else:
|
||||
from pkg_resources import iter_entry_points
|
||||
return [ep.module_name.split(".") for ep in iter_entry_points(group="pytest11")]
|
||||
|
||||
|
||||
@@ -160,7 +181,13 @@ def _iter_pytest_modules(module_context, skip_own_module=False):
|
||||
if Path(file_io.path) != module_context.py__file__():
|
||||
try:
|
||||
m = load_module_from_path(module_context.inference_state, file_io)
|
||||
yield m.as_context()
|
||||
conftest_module = m.as_context()
|
||||
yield conftest_module
|
||||
|
||||
plugins_list = m.tree_node.get_used_names().get("pytest_plugins")
|
||||
if plugins_list:
|
||||
name = conftest_module.create_name(plugins_list[0])
|
||||
yield from _load_pytest_plugins(module_context, name)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
folder = folder.get_parent_folder()
|
||||
@@ -175,6 +202,19 @@ def _iter_pytest_modules(module_context, skip_own_module=False):
|
||||
yield module_value.as_context()
|
||||
|
||||
|
||||
def _load_pytest_plugins(module_context, name):
|
||||
from jedi.inference.helpers import get_str_or_none
|
||||
|
||||
for inferred in name.infer():
|
||||
for seq_value in inferred.py__iter__():
|
||||
for value in seq_value.infer():
|
||||
fq_name = get_str_or_none(value)
|
||||
if fq_name:
|
||||
names = fq_name.split(".")
|
||||
for module_value in module_context.inference_state.import_module(names):
|
||||
yield module_value.as_context()
|
||||
|
||||
|
||||
class FixtureFilter(ParserTreeFilter):
|
||||
def _filter(self, names):
|
||||
for name in super()._filter(names):
|
||||
|
||||
@@ -808,9 +808,11 @@ _implemented = {
|
||||
# https://www.attrs.org/en/stable/names.html
|
||||
'attr': {
|
||||
'define': _dataclass,
|
||||
'frozen': _dataclass,
|
||||
},
|
||||
'attrs': {
|
||||
'define': _dataclass,
|
||||
'frozen': _dataclass,
|
||||
},
|
||||
'os.path': {
|
||||
'dirname': _create_string_input_function(os.path.dirname),
|
||||
|
||||
@@ -143,6 +143,15 @@ This improves autocompletion for libraries that use ``setattr`` or
|
||||
``globals()`` modifications a lot.
|
||||
"""
|
||||
|
||||
allow_unsafe_interpreter_executions = True
|
||||
"""
|
||||
Controls whether descriptors are evaluated when using an Interpreter. This is
|
||||
something you might want to control when using Jedi from a Repl (e.g. IPython)
|
||||
|
||||
Generally this setting allows Jedi to execute __getitem__ and descriptors like
|
||||
`property`.
|
||||
"""
|
||||
|
||||
# ----------------
|
||||
# Caching Validity
|
||||
# ----------------
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
Utilities for end-users.
|
||||
"""
|
||||
|
||||
import __main__ # type: ignore[import]
|
||||
import __main__
|
||||
from collections import namedtuple
|
||||
import logging
|
||||
import traceback
|
||||
|
||||
14
setup.cfg
14
setup.cfg
@@ -21,13 +21,25 @@ per-file-ignores =
|
||||
jedi/__init__.py:F401
|
||||
jedi/inference/compiled/__init__.py:F401
|
||||
jedi/inference/value/__init__.py:F401
|
||||
exclude = jedi/third_party/* .tox/*
|
||||
exclude =
|
||||
.tox/*
|
||||
jedi/third_party/*
|
||||
test/completion/*
|
||||
test/examples/*
|
||||
test/refactor/*
|
||||
test/static_analysis/*
|
||||
|
||||
[pycodestyle]
|
||||
max-line-length = 100
|
||||
|
||||
|
||||
[mypy]
|
||||
# Exclude our copies of external stubs
|
||||
exclude = ^jedi/third_party
|
||||
|
||||
show_error_codes = true
|
||||
enable_error_code = ignore-without-code
|
||||
|
||||
# Ensure generics are explicit about what they are (e.g: `List[str]` rather than
|
||||
# just `List`)
|
||||
disallow_any_generics = True
|
||||
|
||||
21
setup.py
21
setup.py
@@ -27,25 +27,33 @@ setup(name='jedi',
|
||||
maintainer=__AUTHOR__,
|
||||
maintainer_email=__AUTHOR_EMAIL__,
|
||||
url='https://github.com/davidhalter/jedi',
|
||||
project_urls={
|
||||
"Documentation": 'https://jedi.readthedocs.io/en/latest/',
|
||||
},
|
||||
license='MIT',
|
||||
keywords='python completion refactoring vim',
|
||||
long_description=readme,
|
||||
packages=find_packages(exclude=['test', 'test.*']),
|
||||
python_requires='>=3.6',
|
||||
install_requires=['parso>=0.8.0,<0.9.0'],
|
||||
# Python 3.13 grammars are added to parso in 0.8.4
|
||||
install_requires=['parso>=0.8.4,<0.9.0'],
|
||||
extras_require={
|
||||
'testing': [
|
||||
'pytest<7.0.0',
|
||||
'pytest<9.0.0',
|
||||
# docopt for sith doctests
|
||||
'docopt',
|
||||
# coloroma for colored debug output
|
||||
'colorama',
|
||||
'Django<3.1', # For now pin this.
|
||||
'Django',
|
||||
'attrs',
|
||||
],
|
||||
'qa': [
|
||||
'flake8==3.8.3',
|
||||
'mypy==0.782',
|
||||
# latest version supporting Python 3.6
|
||||
'flake8==5.0.4',
|
||||
# latest version supporting Python 3.6
|
||||
'mypy==0.971',
|
||||
# Arbitrary pins, latest at the time of pinning
|
||||
'types-setuptools==67.2.0.1',
|
||||
],
|
||||
'docs': [
|
||||
# Just pin all of these.
|
||||
@@ -91,6 +99,9 @@ setup(name='jedi',
|
||||
'Programming Language :: Python :: 3.8',
|
||||
'Programming Language :: Python :: 3.9',
|
||||
'Programming Language :: Python :: 3.10',
|
||||
'Programming Language :: Python :: 3.11',
|
||||
'Programming Language :: Python :: 3.12',
|
||||
'Programming Language :: Python :: 3.13',
|
||||
'Topic :: Software Development :: Libraries :: Python Modules',
|
||||
'Topic :: Text Editors :: Integrated Development Environments (IDE)',
|
||||
'Topic :: Utilities',
|
||||
|
||||
@@ -413,6 +413,10 @@ with Foo() as f3:
|
||||
with Foo() as f3:
|
||||
f3
|
||||
|
||||
with open("a"), open("b") as bfile:
|
||||
#? ['flush']
|
||||
bfile.flush
|
||||
|
||||
# -----------------
|
||||
# Avoiding multiple definitions
|
||||
# -----------------
|
||||
@@ -420,3 +424,13 @@ with Foo() as f3:
|
||||
some_array = ['', '']
|
||||
#! ['def upper']
|
||||
some_array[some_not_defined_index].upper
|
||||
|
||||
# -----------------
|
||||
# operator
|
||||
# -----------------
|
||||
|
||||
#? bool()
|
||||
res = 'f' in 'foo'; res
|
||||
|
||||
#? bool()
|
||||
res = not {}; res
|
||||
|
||||
@@ -27,3 +27,9 @@ def capsysbinary(capsysbinary):
|
||||
#? ['close']
|
||||
capsysbinary.clos
|
||||
return capsysbinary
|
||||
|
||||
|
||||
# used when fixtures are defined in multiple files
|
||||
pytest_plugins = [
|
||||
"completion.fixture_module",
|
||||
]
|
||||
|
||||
@@ -21,11 +21,9 @@ class Y(X):
|
||||
#? []
|
||||
def __doc__
|
||||
|
||||
# This might or might not be what we wanted, currently properties are also
|
||||
# used like this. IMO this is not wanted ~dave.
|
||||
#? ['__class__']
|
||||
def __class__
|
||||
#? []
|
||||
def __class__
|
||||
#? ['__class__']
|
||||
__class__
|
||||
|
||||
|
||||
|
||||
6
test/completion/fixture_module.py
Normal file
6
test/completion/fixture_module.py
Normal file
@@ -0,0 +1,6 @@
|
||||
# Exists only for completion/pytest.py
|
||||
import pytest
|
||||
|
||||
@pytest.fixture
|
||||
def my_module_fixture():
|
||||
return 1.0
|
||||
@@ -76,7 +76,7 @@ from import_tree.pkg.mod1 import not_existant,
|
||||
#? 22 ['mod1', 'base']
|
||||
from import_tree.pkg. import mod1
|
||||
#? 17 ['mod1', 'mod2', 'random', 'pkg', 'references', 'rename1', 'rename2', 'classes', 'globals', 'recurse_class1', 'recurse_class2', 'invisible_pkg', 'flow_import']
|
||||
from import_tree. import pkg
|
||||
from import_tree. import new_pkg
|
||||
|
||||
#? 18 ['pkg']
|
||||
from import_tree.p import pkg
|
||||
|
||||
@@ -180,6 +180,11 @@ def argskwargs(*args: int, **kwargs: float):
|
||||
#? float()
|
||||
kwargs['']
|
||||
|
||||
class Test:
|
||||
str: str = 'abc'
|
||||
|
||||
#? ['upper']
|
||||
Test.str.upp
|
||||
|
||||
class NotCalledClass:
|
||||
def __init__(self, x):
|
||||
|
||||
26
test/completion/pep0593_annotations.py
Normal file
26
test/completion/pep0593_annotations.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# python >= 3.9
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
# This is just a dummy and very meaningless thing to use with to the Annotated
|
||||
# type hint
|
||||
class Foo:
|
||||
pass
|
||||
|
||||
class A:
|
||||
pass
|
||||
|
||||
|
||||
def annotated_function_params(
|
||||
basic: Annotated[str, Foo()],
|
||||
obj: A,
|
||||
annotated_obj: Annotated[A, Foo()],
|
||||
):
|
||||
#? str()
|
||||
basic
|
||||
|
||||
#? A()
|
||||
obj
|
||||
|
||||
#? A()
|
||||
annotated_obj
|
||||
@@ -96,6 +96,9 @@ def test_x(my_con
|
||||
#? 18 ['my_conftest_fixture']
|
||||
def test_x(my_conftest_fixture):
|
||||
return
|
||||
#? ['my_module_fixture']
|
||||
def test_x(my_modu
|
||||
return
|
||||
|
||||
#? []
|
||||
def lala(my_con
|
||||
|
||||
@@ -321,10 +321,19 @@ def test_docstrings_for_completions(Script):
|
||||
assert isinstance(c.docstring(), str)
|
||||
|
||||
|
||||
def test_completions_order_most_resemblance_on_top(Script):
|
||||
"""Test that the completion which resembles the in-typing the most will come first."""
|
||||
code = "from pathlib import Path\npath = Path('hello.txt')\n\npat"
|
||||
script = Script(code)
|
||||
# User is typing "pat" and "path" is closer to it than "Path".
|
||||
assert ['path', 'Path'] == [comp.name for comp in script.complete()]
|
||||
|
||||
|
||||
def test_fuzzy_completion(Script):
|
||||
script = Script('string = "hello"\nstring.upper')
|
||||
assert ['isupper',
|
||||
'upper'] == [comp.name for comp in script.complete(fuzzy=True)]
|
||||
# 'isupper' is included because it is fuzzily matched.
|
||||
assert ['upper',
|
||||
'isupper'] == [comp.name for comp in script.complete(fuzzy=True)]
|
||||
|
||||
|
||||
def test_math_fuzzy_completion(Script, environment):
|
||||
|
||||
@@ -188,10 +188,7 @@ def test_functions_should_have_params(Script):
|
||||
assert c.get_signatures()
|
||||
|
||||
|
||||
def test_hashlib_params(Script, environment):
|
||||
if environment.version_info < (3,):
|
||||
pytest.skip()
|
||||
|
||||
def test_hashlib_params(Script):
|
||||
script = Script('from hashlib import sha256')
|
||||
c, = script.complete()
|
||||
sig, = c.get_signatures()
|
||||
@@ -351,8 +348,8 @@ def test_parent_on_comprehension(Script):
|
||||
|
||||
def test_type(Script):
|
||||
for c in Script('a = [str()]; a[0].').complete():
|
||||
if c.name == '__class__' and False: # TODO fix.
|
||||
assert c.type == 'class'
|
||||
if c.name == '__class__':
|
||||
assert c.type == 'property'
|
||||
else:
|
||||
assert c.type in ('function', 'statement')
|
||||
|
||||
|
||||
@@ -28,6 +28,11 @@ def test_import_keyword(Script):
|
||||
# unrelated to #44
|
||||
|
||||
|
||||
def test_import_keyword_after_newline(Script):
|
||||
d, = Script("import x\nimport y").help(line=2, column=0)
|
||||
assert d.docstring().startswith('The "import" statement')
|
||||
|
||||
|
||||
def test_import_keyword_with_gotos(goto_or_infer):
|
||||
assert not goto_or_infer("import x", column=0)
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import typing
|
||||
import pytest
|
||||
|
||||
import jedi
|
||||
import jedi.settings
|
||||
from jedi.inference.compiled import mixed
|
||||
from importlib import import_module
|
||||
|
||||
@@ -25,7 +26,7 @@ def get_completion(source, namespace):
|
||||
|
||||
|
||||
def test_builtin_details():
|
||||
import keyword
|
||||
import keyword # noqa: F401
|
||||
|
||||
class EmptyClass:
|
||||
pass
|
||||
@@ -52,9 +53,9 @@ def test_numpy_like_non_zero():
|
||||
"""
|
||||
|
||||
class NumpyNonZero:
|
||||
|
||||
def __zero__(self):
|
||||
raise ValueError('Numpy arrays would raise and tell you to use .any() or all()')
|
||||
|
||||
def __bool__(self):
|
||||
raise ValueError('Numpy arrays would raise and tell you to use .any() or all()')
|
||||
|
||||
@@ -101,26 +102,28 @@ def test_side_effect_completion():
|
||||
assert foo.name == 'foo'
|
||||
|
||||
|
||||
def _assert_interpreter_complete(source, namespace, completions,
|
||||
**kwds):
|
||||
def _assert_interpreter_complete(source, namespace, completions, *, check_type=False, **kwds):
|
||||
script = jedi.Interpreter(source, [namespace], **kwds)
|
||||
cs = script.complete()
|
||||
actual = [c.name for c in cs]
|
||||
if check_type:
|
||||
for c in cs:
|
||||
c.type
|
||||
assert sorted(actual) == sorted(completions)
|
||||
|
||||
|
||||
def test_complete_raw_function():
|
||||
from os.path import join
|
||||
from os.path import join # noqa: F401
|
||||
_assert_interpreter_complete('join("").up', locals(), ['upper'])
|
||||
|
||||
|
||||
def test_complete_raw_function_different_name():
|
||||
from os.path import join as pjoin
|
||||
from os.path import join as pjoin # noqa: F401
|
||||
_assert_interpreter_complete('pjoin("").up', locals(), ['upper'])
|
||||
|
||||
|
||||
def test_complete_raw_module():
|
||||
import os
|
||||
import os # noqa: F401
|
||||
_assert_interpreter_complete('os.path.join("a").up', locals(), ['upper'])
|
||||
|
||||
|
||||
@@ -219,7 +222,7 @@ def test__getattr__completions(allow_unsafe_getattr, class_is_findable):
|
||||
|
||||
@pytest.fixture(params=[False, True])
|
||||
def allow_unsafe_getattr(request, monkeypatch):
|
||||
monkeypatch.setattr(jedi.Interpreter, '_allow_descriptor_getattr_default', request.param)
|
||||
monkeypatch.setattr(jedi.settings, 'allow_unsafe_interpreter_executions', request.param)
|
||||
return request.param
|
||||
|
||||
|
||||
@@ -278,7 +281,7 @@ def test_param_completion():
|
||||
def foo(bar):
|
||||
pass
|
||||
|
||||
lambd = lambda xyz: 3
|
||||
lambd = lambda xyz: 3 # noqa: E731
|
||||
|
||||
_assert_interpreter_complete('foo(bar', locals(), ['bar='])
|
||||
_assert_interpreter_complete('lambd(xyz', locals(), ['xyz='])
|
||||
@@ -292,7 +295,7 @@ def test_endless_yield():
|
||||
|
||||
|
||||
def test_completion_params():
|
||||
foo = lambda a, b=3: None
|
||||
foo = lambda a, b=3: None # noqa: E731
|
||||
|
||||
script = jedi.Interpreter('foo', [locals()])
|
||||
c, = script.complete()
|
||||
@@ -307,8 +310,9 @@ def test_completion_param_annotations():
|
||||
# Need to define this function not directly in Python. Otherwise Jedi is too
|
||||
# clever and uses the Python code instead of the signature object.
|
||||
code = 'def foo(a: 1, b: str, c: int = 1.0) -> bytes: pass'
|
||||
exec(code, locals())
|
||||
script = jedi.Interpreter('foo', [locals()])
|
||||
exec_locals = {}
|
||||
exec(code, exec_locals)
|
||||
script = jedi.Interpreter('foo', [exec_locals])
|
||||
c, = script.complete()
|
||||
sig, = c.get_signatures()
|
||||
a, b, c = sig.params
|
||||
@@ -320,7 +324,7 @@ def test_completion_param_annotations():
|
||||
assert b.description == 'param b: str'
|
||||
assert c.description == 'param c: int=1.0'
|
||||
|
||||
d, = jedi.Interpreter('foo()', [locals()]).infer()
|
||||
d, = jedi.Interpreter('foo()', [exec_locals]).infer()
|
||||
assert d.name == 'bytes'
|
||||
|
||||
|
||||
@@ -406,7 +410,7 @@ def test_dir_magic_method(allow_unsafe_getattr):
|
||||
def test_name_not_findable():
|
||||
class X():
|
||||
if 0:
|
||||
NOT_FINDABLE
|
||||
NOT_FINDABLE # noqa: F821
|
||||
|
||||
def hidden(self):
|
||||
return
|
||||
@@ -419,7 +423,7 @@ def test_name_not_findable():
|
||||
|
||||
|
||||
def test_stubs_working():
|
||||
from multiprocessing import cpu_count
|
||||
from multiprocessing import cpu_count # noqa: F401
|
||||
defs = jedi.Interpreter("cpu_count()", [locals()]).infer()
|
||||
assert [d.name for d in defs] == ['int']
|
||||
|
||||
@@ -522,14 +526,17 @@ def test_partial_signatures(code, expected, index):
|
||||
c = functools.partial(func, 1, c=2)
|
||||
|
||||
sig, = jedi.Interpreter(code, [locals()]).get_signatures()
|
||||
assert sig.name == 'partial'
|
||||
assert [p.name for p in sig.params] == expected
|
||||
assert index == sig.index
|
||||
|
||||
if sys.version_info < (3, 13):
|
||||
# Python 3.13.0b3 makes functools.partial be a descriptor, which breaks
|
||||
# Jedi's `py__name__` detection; see https://github.com/davidhalter/jedi/issues/2012
|
||||
assert sig.name == 'partial'
|
||||
|
||||
|
||||
def test_type_var():
|
||||
"""This was an issue before, see Github #1369"""
|
||||
import typing
|
||||
x = typing.TypeVar('myvar')
|
||||
def_, = jedi.Interpreter('x', [locals()]).infer()
|
||||
assert def_.name == 'TypeVar'
|
||||
@@ -610,12 +617,12 @@ def test_dict_getitem(code, types):
|
||||
# ('for x in dunder: x', 'str'),
|
||||
]
|
||||
)
|
||||
def test_dunders(class_is_findable, code, expected):
|
||||
def test_dunders(class_is_findable, code, expected, allow_unsafe_getattr):
|
||||
from typing import Iterator
|
||||
|
||||
class DunderCls:
|
||||
def __getitem__(self, key) -> int:
|
||||
pass
|
||||
return 1
|
||||
|
||||
def __iter__(self, key) -> Iterator[str]:
|
||||
pass
|
||||
@@ -656,10 +663,12 @@ def bar():
|
||||
({'return': 'typing.Union[str, int]'}, ['int', 'str'], ''),
|
||||
({'return': 'typing.Union["str", int]'},
|
||||
['int', 'str'] if sys.version_info >= (3, 9) else ['int'], ''),
|
||||
({'return': 'typing.Union["str", 1]'}, [], ''),
|
||||
({'return': 'typing.Union["str", 1]'},
|
||||
['str'] if sys.version_info >= (3, 11) else [], ''),
|
||||
({'return': 'typing.Optional[str]'}, ['NoneType', 'str'], ''),
|
||||
({'return': 'typing.Optional[str, int]'}, [], ''), # Takes only one arg
|
||||
({'return': 'typing.Any'}, [], ''),
|
||||
({'return': 'typing.Any'},
|
||||
['_AnyMeta'] if sys.version_info >= (3, 11) else [], ''),
|
||||
|
||||
({'return': 'typing.Tuple[int, str]'},
|
||||
['Tuple' if sys.version_info[:2] == (3, 6) else 'tuple'], ''),
|
||||
@@ -682,7 +691,7 @@ def bar():
|
||||
]
|
||||
)
|
||||
def test_string_annotation(annotations, result, code):
|
||||
x = lambda foo: 1
|
||||
x = lambda foo: 1 # noqa: E731
|
||||
x.__annotations__ = annotations
|
||||
defs = jedi.Interpreter(code or 'x()', [locals()]).infer()
|
||||
assert [d.name for d in defs] == result
|
||||
@@ -751,6 +760,102 @@ def test_param_infer_default():
|
||||
],
|
||||
)
|
||||
def test_keyword_param_completion(code, expected):
|
||||
import random
|
||||
import random # noqa: F401
|
||||
completions = jedi.Interpreter(code, [locals()]).complete()
|
||||
assert expected == [c.name for c in completions if c.name.endswith('=')]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('class_is_findable', [False, True])
|
||||
def test_avoid_descriptor_executions_if_not_necessary(class_is_findable):
|
||||
counter = 0
|
||||
|
||||
class AvoidDescriptor(object):
|
||||
@property
|
||||
def prop(self):
|
||||
nonlocal counter
|
||||
counter += 1
|
||||
return self
|
||||
|
||||
if not class_is_findable:
|
||||
AvoidDescriptor.__name__ = "something_somewhere"
|
||||
namespace = {'b': AvoidDescriptor()}
|
||||
expected = ['prop']
|
||||
_assert_interpreter_complete('b.pro', namespace, expected, check_type=True)
|
||||
assert counter == 0
|
||||
_assert_interpreter_complete('b.prop.pro', namespace, expected, check_type=True)
|
||||
assert counter == 1
|
||||
|
||||
|
||||
class Hello:
|
||||
its_me = 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize('class_is_findable', [False, True])
|
||||
def test_try_to_use_return_annotation_for_property(class_is_findable):
|
||||
class WithProperties(object):
|
||||
@property
|
||||
def with_annotation1(self) -> str:
|
||||
raise BaseException
|
||||
|
||||
@property
|
||||
def with_annotation2(self) -> 'str':
|
||||
raise BaseException
|
||||
|
||||
@property
|
||||
def with_annotation3(self) -> Hello:
|
||||
raise BaseException
|
||||
|
||||
@property
|
||||
def with_annotation4(self) -> 'Hello':
|
||||
raise BaseException
|
||||
|
||||
@property
|
||||
def with_annotation_garbage1(self) -> 'asldjflksjdfljdslkjfsl': # noqa
|
||||
return Hello()
|
||||
|
||||
@property
|
||||
def with_annotation_garbage2(self) -> 'sdf$@@$5*+8': # noqa
|
||||
return Hello()
|
||||
|
||||
@property
|
||||
def without_annotation(self):
|
||||
return ""
|
||||
|
||||
if not class_is_findable:
|
||||
WithProperties.__name__ = "something_somewhere"
|
||||
Hello.__name__ = "something_somewhere_else"
|
||||
|
||||
namespace = {'p': WithProperties()}
|
||||
_assert_interpreter_complete('p.without_annotation.upp', namespace, ['upper'])
|
||||
_assert_interpreter_complete('p.with_annotation1.upp', namespace, ['upper'])
|
||||
_assert_interpreter_complete('p.with_annotation2.upp', namespace, ['upper'])
|
||||
_assert_interpreter_complete('p.with_annotation3.its', namespace, ['its_me'])
|
||||
_assert_interpreter_complete('p.with_annotation4.its', namespace, ['its_me'])
|
||||
# This is a fallback, if the annotations don't help
|
||||
_assert_interpreter_complete('p.with_annotation_garbage1.its', namespace, ['its_me'])
|
||||
_assert_interpreter_complete('p.with_annotation_garbage2.its', namespace, ['its_me'])
|
||||
|
||||
|
||||
def test_nested__getitem__():
|
||||
d = {'foo': {'bar': 1}}
|
||||
_assert_interpreter_complete('d["fo', locals(), ['"foo"'])
|
||||
_assert_interpreter_complete('d["foo"]["ba', locals(), ['"bar"'])
|
||||
_assert_interpreter_complete('(d["foo"])["ba', locals(), ['"bar"'])
|
||||
_assert_interpreter_complete('((d["foo"]))["ba', locals(), ['"bar"'])
|
||||
|
||||
|
||||
@pytest.mark.parametrize('class_is_findable', [False, True])
|
||||
def test_custom__getitem__(class_is_findable, allow_unsafe_getattr):
|
||||
class CustomGetItem:
|
||||
def __getitem__(self, x: int):
|
||||
return "asdf"
|
||||
|
||||
if not class_is_findable:
|
||||
CustomGetItem.__name__ = "something_somewhere"
|
||||
|
||||
namespace = {'c': CustomGetItem()}
|
||||
if not class_is_findable and not allow_unsafe_getattr:
|
||||
expected = []
|
||||
else:
|
||||
expected = ['upper']
|
||||
_assert_interpreter_complete('c["a"].up', namespace, expected)
|
||||
|
||||
@@ -144,13 +144,17 @@ def test_load_save_project(tmpdir):
|
||||
]
|
||||
)
|
||||
def test_search(string, full_names, kwargs):
|
||||
some_search_test_var = 1.0
|
||||
some_search_test_var = 1.0 # noqa: F841
|
||||
project = Project(test_dir)
|
||||
if kwargs.pop('complete', False) is True:
|
||||
defs = project.complete_search(string, **kwargs)
|
||||
else:
|
||||
defs = project.search(string, **kwargs)
|
||||
assert sorted([('stub:' if d.is_stub() else '') + (d.full_name or d.name) for d in defs]) == full_names
|
||||
actual = sorted([
|
||||
('stub:' if d.is_stub() else '') + (d.full_name or d.name)
|
||||
for d in defs
|
||||
])
|
||||
assert actual == full_names
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import os
|
||||
import shutil
|
||||
from textwrap import dedent
|
||||
from pathlib import Path
|
||||
import platform
|
||||
@@ -6,6 +7,7 @@ import platform
|
||||
import pytest
|
||||
|
||||
import jedi
|
||||
from test.helpers import get_example_dir
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
@@ -52,6 +54,47 @@ def test_rename_mod(Script, dir_with_content):
|
||||
''').format(dir=dir_with_content)
|
||||
|
||||
|
||||
@pytest.mark.skipif('sys.version_info[:2] < (3, 8)', message="Python 3.8 introduces dirs_exist_ok")
|
||||
def test_namespace_package(Script, tmpdir):
|
||||
origin = get_example_dir('implicit_namespace_package')
|
||||
shutil.copytree(origin, tmpdir.strpath, dirs_exist_ok=True)
|
||||
sys_path = [
|
||||
os.path.join(tmpdir.strpath, 'ns1'),
|
||||
os.path.join(tmpdir.strpath, 'ns2')
|
||||
]
|
||||
script_path = os.path.join(tmpdir.strpath, 'script.py')
|
||||
script = Script(
|
||||
'import pkg\n',
|
||||
path=script_path,
|
||||
project=jedi.Project(os.path.join(tmpdir.strpath, 'does-not-exist'), sys_path=sys_path),
|
||||
)
|
||||
refactoring = script.rename(line=1, new_name='new_pkg')
|
||||
refactoring.apply()
|
||||
old1 = os.path.join(sys_path[0], "pkg")
|
||||
new1 = os.path.join(sys_path[0], "new_pkg")
|
||||
old2 = os.path.join(sys_path[1], "pkg")
|
||||
new2 = os.path.join(sys_path[1], "new_pkg")
|
||||
assert not os.path.exists(old1)
|
||||
assert os.path.exists(new1)
|
||||
assert not os.path.exists(old2)
|
||||
assert os.path.exists(new2)
|
||||
|
||||
changed, = iter(refactoring.get_changed_files().values())
|
||||
assert changed.get_new_code() == "import new_pkg\n"
|
||||
|
||||
assert refactoring.get_diff() == dedent(f'''\
|
||||
rename from {old1}
|
||||
rename to {new1}
|
||||
rename from {old2}
|
||||
rename to {new2}
|
||||
--- {script_path}
|
||||
+++ {script_path}
|
||||
@@ -1,2 +1,2 @@
|
||||
-import pkg
|
||||
+import new_pkg
|
||||
''').format(dir=dir_with_content)
|
||||
|
||||
|
||||
def test_rename_none_path(Script):
|
||||
refactoring = Script('foo', path=None).rename(new_name='bar')
|
||||
with pytest.raises(jedi.RefactoringError, match='on a Script with path=None'):
|
||||
@@ -70,7 +113,7 @@ def test_diff_without_ending_newline(Script):
|
||||
b
|
||||
-a
|
||||
+c
|
||||
''')
|
||||
''') # noqa: W291
|
||||
|
||||
|
||||
def test_diff_path_outside_of_project(Script):
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import os
|
||||
import sys
|
||||
import sys # noqa: F401
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import jedi
|
||||
from jedi import debug
|
||||
|
||||
|
||||
def test_simple():
|
||||
jedi.set_debug_function()
|
||||
debug.speed('foo')
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
from textwrap import dedent
|
||||
import sys
|
||||
import math
|
||||
from collections import Counter
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
import jedi
|
||||
from jedi.inference import compiled
|
||||
from jedi.inference.compiled.access import DirectObjectAccess
|
||||
from jedi.inference.gradual.conversion import _stub_to_python_value_set
|
||||
@@ -26,7 +28,10 @@ def test_builtin_loading(inference_state):
|
||||
assert not from_name.py__doc__() # It's a stub
|
||||
|
||||
|
||||
def test_next_docstr(inference_state):
|
||||
def test_next_docstr(inference_state, environment):
|
||||
if environment.version_info[:2] != sys.version_info[:2]:
|
||||
pytest.skip()
|
||||
|
||||
next_ = compiled.builtin_from_name(inference_state, 'next')
|
||||
assert next_.tree_node is not None
|
||||
assert next_.py__doc__() == '' # It's a stub
|
||||
@@ -77,9 +82,9 @@ def test_method_completion(Script, environment):
|
||||
assert [c.name for c in Script(code).complete()] == ['__func__']
|
||||
|
||||
|
||||
def test_time_docstring(Script):
|
||||
def test_time_docstring():
|
||||
import time
|
||||
comp, = Script('import time\ntime.sleep').complete()
|
||||
comp, = jedi.Script('import time\ntime.sleep').complete()
|
||||
assert comp.docstring(raw=True) == time.sleep.__doc__
|
||||
expected = 'sleep(secs: float) -> None\n\n' + time.sleep.__doc__
|
||||
assert comp.docstring() == expected
|
||||
|
||||
@@ -206,6 +206,7 @@ def test_numpydoc_parameters_set_of_values():
|
||||
assert 'capitalize' in names
|
||||
assert 'numerator' in names
|
||||
|
||||
|
||||
@pytest.mark.skipif(numpydoc_unavailable,
|
||||
reason='numpydoc module is unavailable')
|
||||
def test_numpydoc_parameters_set_single_value():
|
||||
@@ -390,7 +391,8 @@ def test_numpydoc_yields():
|
||||
@pytest.mark.skipif(numpydoc_unavailable or numpy_unavailable,
|
||||
reason='numpydoc or numpy module is unavailable')
|
||||
def test_numpy_returns():
|
||||
s = dedent('''
|
||||
s = dedent(
|
||||
'''
|
||||
import numpy
|
||||
x = numpy.asarray([])
|
||||
x.d'''
|
||||
@@ -402,7 +404,8 @@ def test_numpy_returns():
|
||||
@pytest.mark.skipif(numpydoc_unavailable or numpy_unavailable,
|
||||
reason='numpydoc or numpy module is unavailable')
|
||||
def test_numpy_comp_returns():
|
||||
s = dedent('''
|
||||
s = dedent(
|
||||
'''
|
||||
import numpy
|
||||
x = numpy.array([])
|
||||
x.d'''
|
||||
|
||||
@@ -33,7 +33,9 @@ def test_get_signatures_stdlib(Script):
|
||||
|
||||
# Check only on linux 64 bit platform and Python3.8.
|
||||
@pytest.mark.parametrize('load_unsafe_extensions', [False, True])
|
||||
@pytest.mark.skipif('sys.platform != "linux" or sys.maxsize <= 2**32 or sys.version_info[:2] != (3, 8)')
|
||||
@pytest.mark.skipif(
|
||||
'sys.platform != "linux" or sys.maxsize <= 2**32 or sys.version_info[:2] != (3, 8)',
|
||||
)
|
||||
def test_init_extension_module(Script, load_unsafe_extensions):
|
||||
"""
|
||||
``__init__`` extension modules are also packages and Jedi should understand
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
import pytest
|
||||
from textwrap import dedent
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def skip_not_supported(environment):
|
||||
if environment.version_info < (3, 6):
|
||||
pytest.skip()
|
||||
|
||||
|
||||
def test_fstring_multiline(Script):
|
||||
code = dedent("""\
|
||||
'' f'''s{
|
||||
|
||||
@@ -222,7 +222,7 @@ def test_goto_stubs_on_itself(Script, code, type_):
|
||||
|
||||
def test_module_exists_only_as_stub(Script):
|
||||
try:
|
||||
import redis
|
||||
import redis # noqa: F401
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
@@ -234,7 +234,7 @@ def test_module_exists_only_as_stub(Script):
|
||||
|
||||
def test_django_exists_only_as_stub(Script):
|
||||
try:
|
||||
import django
|
||||
import django # noqa: F401
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
|
||||
@@ -307,6 +307,28 @@ def test_os_issues(Script):
|
||||
assert 'path' in import_names(s, column=len(s) - 3)
|
||||
|
||||
|
||||
def test_duplicated_import(Script):
|
||||
def import_names(*args, **kwargs):
|
||||
return [d.name for d in Script(*args).complete(**kwargs)]
|
||||
|
||||
s = 'import os, o'
|
||||
assert 'os' not in import_names(s)
|
||||
assert 'os' in import_names(s, column=len(s) - 3)
|
||||
|
||||
s = 'from os import path, p'
|
||||
assert 'path' not in import_names(s)
|
||||
assert 'path' in import_names(s, column=len(s) - 3)
|
||||
|
||||
s = 'import path as pp, p'
|
||||
assert 'path' not in import_names(s)
|
||||
|
||||
s = 'from os import path as pp, p'
|
||||
assert 'path' not in import_names(s)
|
||||
|
||||
s = 'from os import chdir as path, p'
|
||||
assert 'path' in import_names(s)
|
||||
|
||||
|
||||
def test_path_issues(Script):
|
||||
"""
|
||||
See pull request #684 for details.
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import pytest
|
||||
from jedi.inference.value import TreeInstance
|
||||
|
||||
|
||||
@@ -12,29 +11,23 @@ def _infer_literal(Script, code, is_fstring=False):
|
||||
return def_._name._value.get_safe_value()
|
||||
|
||||
|
||||
def test_f_strings(Script, environment):
|
||||
def test_f_strings(Script):
|
||||
"""
|
||||
f literals are not really supported in Jedi. They just get ignored and an
|
||||
empty string is returned.
|
||||
"""
|
||||
if environment.version_info < (3, 6):
|
||||
pytest.skip()
|
||||
|
||||
assert _infer_literal(Script, 'f"asdf"', is_fstring=True) == ''
|
||||
assert _infer_literal(Script, 'f"{asdf} "', is_fstring=True) == ''
|
||||
assert _infer_literal(Script, 'F"{asdf} "', is_fstring=True) == ''
|
||||
assert _infer_literal(Script, 'rF"{asdf} "', is_fstring=True) == ''
|
||||
|
||||
|
||||
def test_rb_strings(Script, environment):
|
||||
def test_rb_strings(Script):
|
||||
assert _infer_literal(Script, 'x = br"asdf"; x') == b'asdf'
|
||||
assert _infer_literal(Script, 'x = rb"asdf"; x') == b'asdf'
|
||||
|
||||
|
||||
def test_thousand_separators(Script, environment):
|
||||
if environment.version_info < (3, 6):
|
||||
pytest.skip()
|
||||
|
||||
def test_thousand_separators(Script):
|
||||
assert _infer_literal(Script, '1_2_3') == 123
|
||||
assert _infer_literal(Script, '123_456_789') == 123456789
|
||||
assert _infer_literal(Script, '0x3_4') == 52
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from textwrap import dedent
|
||||
from operator import ge, lt
|
||||
from operator import eq, ge, lt
|
||||
import re
|
||||
import os
|
||||
|
||||
@@ -13,18 +13,16 @@ from ..helpers import get_example_dir
|
||||
'code, sig, names, op, version', [
|
||||
('import math; math.cos', 'cos(x, /)', ['x'], ge, (3, 6)),
|
||||
|
||||
('next', 'next(iterator, default=None, /)', ['iterator', 'default'], ge, (3, 6)),
|
||||
('next', 'next(iterator, default=None, /)', ['iterator', 'default'], lt, (3, 12)),
|
||||
('next', 'next()', [], eq, (3, 12)),
|
||||
('next', 'next(iterator, default=None, /)', ['iterator', 'default'], ge, (3, 13)),
|
||||
|
||||
('str', "str(object='', /) -> str", ['object'], ge, (3, 6)),
|
||||
|
||||
('pow', 'pow(x, y, z=None, /) -> number', ['x', 'y', 'z'], lt, (3, 6)),
|
||||
('pow', 'pow(base, exp, mod=None)', ['base', 'exp', 'mod'], ge, (3, 8)),
|
||||
|
||||
('bytes.partition', 'partition(self, sep, /) -> (head, sep, tail)',
|
||||
['self', 'sep'], lt, (3, 6)),
|
||||
('bytes.partition', 'partition(self, sep, /)', ['self', 'sep'], ge, (3, 6)),
|
||||
|
||||
('bytes().partition', 'partition(sep, /) -> (head, sep, tail)', ['sep'], lt, (3, 6)),
|
||||
('bytes().partition', 'partition(sep, /)', ['sep'], ge, (3, 6)),
|
||||
]
|
||||
)
|
||||
@@ -355,6 +353,7 @@ def test_dataclass_signature(Script, skip_pre_python37, start, start_params):
|
||||
price, = sig.params[-2].infer()
|
||||
assert price.name == 'float'
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'start, start_params', [
|
||||
['@define\nclass X:', []],
|
||||
|
||||
@@ -30,14 +30,16 @@ def test_paths_from_assignment(Script):
|
||||
assert paths('sys.path, other = ["a"], 2') == set()
|
||||
|
||||
|
||||
def test_venv_and_pths(venv_path):
|
||||
def test_venv_and_pths(venv_path, environment):
|
||||
pjoin = os.path.join
|
||||
|
||||
site_pkg_path = pjoin(venv_path, 'lib')
|
||||
if os.name == 'nt':
|
||||
site_pkg_path = pjoin(site_pkg_path, 'site-packages')
|
||||
if environment.version_info < (3, 11):
|
||||
site_pkg_path = pjoin(venv_path, 'lib', 'site-packages')
|
||||
else:
|
||||
site_pkg_path = glob(pjoin(site_pkg_path, 'python*', 'site-packages'))[0]
|
||||
site_pkg_path = pjoin(venv_path, 'Lib', 'site-packages')
|
||||
else:
|
||||
site_pkg_path = glob(pjoin(venv_path, 'lib', 'python*', 'site-packages'))[0]
|
||||
shutil.rmtree(site_pkg_path)
|
||||
shutil.copytree(get_example_dir('sample_venvs', 'pth_directory'), site_pkg_path)
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import os
|
||||
import sys
|
||||
from collections import namedtuple
|
||||
|
||||
import pytest
|
||||
@@ -50,8 +51,26 @@ def test_completion(case, monkeypatch, environment, has_django):
|
||||
pytest_plugin_dir = str(helpers.get_example_dir("pytest_plugin_package"))
|
||||
case._project.added_sys_path = [pytest_plugin_dir]
|
||||
|
||||
# ... and mock setuptools entry points to include it
|
||||
# ... and mock the entry points to include it
|
||||
# see https://docs.pytest.org/en/stable/how-to/writing_plugins.html#setuptools-entry-points
|
||||
if sys.version_info >= (3, 8):
|
||||
def mock_entry_points(*, group=None):
|
||||
import importlib.metadata
|
||||
entries = [importlib.metadata.EntryPoint(
|
||||
name=None,
|
||||
value="pytest_plugin.plugin",
|
||||
group="pytest11",
|
||||
)]
|
||||
|
||||
if sys.version_info >= (3, 10):
|
||||
assert group == "pytest11"
|
||||
return entries
|
||||
else:
|
||||
assert group is None
|
||||
return {"pytest11": entries}
|
||||
|
||||
monkeypatch.setattr("importlib.metadata.entry_points", mock_entry_points)
|
||||
else:
|
||||
def mock_iter_entry_points(group):
|
||||
assert group == "pytest11"
|
||||
EntryPoint = namedtuple("EntryPoint", ["module_name"])
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
from textwrap import dedent
|
||||
|
||||
import pytest
|
||||
from parso import parse
|
||||
|
||||
|
||||
|
||||
@@ -12,8 +12,8 @@ class TestSetupReadline(unittest.TestCase):
|
||||
class NameSpace(object):
|
||||
pass
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
def setUp(self, *args, **kwargs):
|
||||
super().setUp(*args, **kwargs)
|
||||
|
||||
self.namespace = self.NameSpace()
|
||||
utils.setup_readline(self.namespace)
|
||||
@@ -73,15 +73,21 @@ class TestSetupReadline(unittest.TestCase):
|
||||
import os
|
||||
s = 'from os import '
|
||||
goal = {s + el for el in dir(os)}
|
||||
|
||||
# There are minor differences, e.g. the dir doesn't include deleted
|
||||
# items as well as items that are not only available on linux.
|
||||
difference = set(self.complete(s)).symmetric_difference(goal)
|
||||
ACCEPTED_DIFFERENCE_PREFIXES = [
|
||||
'_', 'O_', 'EX_', 'EFD_', 'MFD_', 'TFD_',
|
||||
'SF_', 'ST_', 'CLD_', 'POSIX_SPAWN_', 'P_',
|
||||
'RWF_', 'CLONE_', 'SCHED_', 'SPLICE_',
|
||||
]
|
||||
difference = {
|
||||
x for x in difference
|
||||
if all(not x.startswith('from os import ' + s)
|
||||
for s in ['_', 'O_', 'EX_', 'MFD_', 'SF_', 'ST_',
|
||||
'CLD_', 'POSIX_SPAWN_', 'P_', 'RWF_',
|
||||
'SCHED_'])
|
||||
if not any(
|
||||
x.startswith('from os import ' + prefix)
|
||||
for prefix in ACCEPTED_DIFFERENCE_PREFIXES
|
||||
)
|
||||
}
|
||||
# There are quite a few differences, because both Windows and Linux
|
||||
# (posix and nt) libraries are included.
|
||||
|
||||
Reference in New Issue
Block a user