Skip to content

Commit

Permalink
Merge 19b8eae into 9d1587a
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJCLaw committed Jul 26, 2020
2 parents 9d1587a + 19b8eae commit 52bbb3e
Show file tree
Hide file tree
Showing 28 changed files with 128 additions and 59 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -30,8 +30,8 @@ matrix:
install:
- 'pip install .[qa]'
script:
# Ignore F401, which are unused imports. flake8 is a primitive tool and is sometimes wrong.
- 'flake8 --extend-ignore F401 jedi setup.py'
- 'flake8 jedi setup.py'
- 'mypy jedi sith.py'
install:
- sudo apt-get -y install python3-venv
- pip install .[testing]
Expand Down
18 changes: 8 additions & 10 deletions jedi/api/classes.py
Expand Up @@ -15,18 +15,18 @@
"""
import re
import warnings
from pathlib import Path
from typing import Optional

from parso.python.tree import search_ancestor
from parso.tree import search_ancestor

from jedi import settings
from jedi import debug
from jedi.inference.utils import unite
from jedi.cache import memoize_method
from jedi.inference import imports
from jedi.inference.imports import ImportName
from jedi.inference.compiled.mixed import MixedName
from jedi.inference.gradual.typeshed import StubModuleValue
from jedi.inference.names import ImportName, SubModuleName
from jedi.inference.gradual.stub_value import StubModuleValue
from jedi.inference.gradual.conversion import convert_names, convert_values
from jedi.inference.base_value import ValueSet
from jedi.api.keywords import KeywordName
Expand Down Expand Up @@ -93,17 +93,15 @@ def _get_module_context(self):
return self._name.get_root_context()

@property
def module_path(self) -> Optional[str]:
def module_path(self) -> Optional[Path]:
"""
Shows the file path of a module. e.g. ``/usr/lib/python3.9/os.py``
:rtype: str or None
"""
module = self._get_module_context()
if module.is_stub() or not module.is_compiled():
# Compiled modules should not return a module path even if they
# have one.
path = self._get_module_context().py__file__()
path: Optional[Path] = self._get_module_context().py__file__()
if path is not None:
return path

Expand Down Expand Up @@ -186,7 +184,7 @@ def type(self):
tree_name.is_definition():
resolve = True

if isinstance(self._name, imports.SubModuleName) or resolve:
if isinstance(self._name, SubModuleName) or resolve:
for value in self._name.infer():
return value.api_type
return self._name.api_type
Expand Down Expand Up @@ -497,7 +495,7 @@ def infer(self, *, only_stubs=False, prefer_stubs=False):
return [self if n == self._name else Name(self._inference_state, n)
for n in resulting_names]

@property
@property # type: ignore[misc]
@memoize_method
def params(self):
warnings.warn(
Expand Down
14 changes: 10 additions & 4 deletions jedi/api/completion_cache.py
@@ -1,16 +1,22 @@
_cache = {}
from typing import Dict, Tuple, Callable

CacheValues = Tuple[str, str, str]
CacheValuesCallback = Callable[[], CacheValues]

def save_entry(module_name, name, cache):

_cache: Dict[str, Dict[str, CacheValues]] = {}


def save_entry(module_name: str, name: str, cache: CacheValues) -> None:
try:
module_cache = _cache[module_name]
except KeyError:
module_cache = _cache[module_name] = {}
module_cache[name] = cache


def _create_get_from_cache(number):
def _get_from_cache(module_name, name, get_cache_values):
def _create_get_from_cache(number: int) -> Callable[[str, str, CacheValuesCallback], str]:
def _get_from_cache(module_name: str, name: str, get_cache_values: CacheValuesCallback) -> str:
try:
return _cache[module_name][name][number]
except KeyError:
Expand Down
3 changes: 2 additions & 1 deletion jedi/api/environment.py
Expand Up @@ -384,7 +384,8 @@ def _get_executable_path(path, safe=True):


def _get_executables_from_windows_registry(version):
import winreg
# https://github.com/python/typeshed/pull/3794 adds winreg
import winreg # type: ignore[import]

# TODO: support Python Anaconda.
sub_keys = [
Expand Down
7 changes: 5 additions & 2 deletions jedi/api/keywords.py
@@ -1,10 +1,13 @@
import pydoc
from contextlib import suppress
from typing import Dict, Optional

from jedi.inference.names import AbstractArbitraryName

try:
from pydoc_data import topics as pydoc_topics
# https://github.com/python/typeshed/pull/4351 adds pydoc_data
from pydoc_data import topics # type: ignore[import]
pydoc_topics: Optional[Dict[str, str]] = topics.topics
except ImportError:
# Python 3.6.8 embeddable does not have pydoc_data.
pydoc_topics = None
Expand Down Expand Up @@ -44,6 +47,6 @@ def get_target(s):
return ''

try:
return pydoc_topics.topics[label].strip() if pydoc_topics else ''
return pydoc_topics[label].strip() if pydoc_topics else ''
except KeyError:
return ''
5 changes: 3 additions & 2 deletions jedi/cache.py
Expand Up @@ -13,14 +13,15 @@
"""
import time
from functools import wraps
from typing import Any, Dict, Tuple

from jedi import settings
from parso.cache import parser_cache

_time_caches = {}
_time_caches: Dict[str, Dict[Any, Tuple[float, Any]]] = {}


def clear_time_caches(delete_all=False):
def clear_time_caches(delete_all: bool = False) -> None:
""" Jedi caches many things, that should be completed after each completion
finishes.
Expand Down
4 changes: 2 additions & 2 deletions jedi/debug.py
Expand Up @@ -20,7 +20,7 @@ def _lazy_colorama_init():
raise ImportError
else:
# Use colorama for nicer console output.
from colorama import Fore, init
from colorama import Fore, init # type: ignore[import]
from colorama import initialise

def _lazy_colorama_init(): # noqa: F811
Expand All @@ -45,7 +45,7 @@ def _lazy_colorama_init(): # noqa: F811
_inited = True

except ImportError:
class Fore:
class Fore: # type: ignore[no-redef]
RED = ''
GREEN = ''
YELLOW = ''
Expand Down
5 changes: 3 additions & 2 deletions jedi/inference/__init__.py
Expand Up @@ -120,14 +120,15 @@ def execute(value, arguments):
debug.dbg('execute result: %s in %s', value_set, value)
return value_set

@property
# mypy doesn't suppport decorated propeties (https://github.com/python/mypy/issues/1362)
@property # type: ignore[misc]
@inference_state_function_cache()
def builtins_module(self):
module_name = 'builtins'
builtins_module, = self.import_module((module_name,), sys_path=())
return builtins_module

@property
@property # type: ignore[misc]
@inference_state_function_cache()
def typing_module(self):
typing_module, = self.import_module(('typing',))
Expand Down
3 changes: 3 additions & 0 deletions jedi/inference/compiled/__init__.py
@@ -1,3 +1,6 @@
# This file also re-exports symbols for wider use. We configure mypy and flake8
# to be aware that this file does this.

from jedi.inference.compiled.value import CompiledValue, CompiledName, \
CompiledValueFilter, CompiledValueName, create_from_access_path
from jedi.inference.base_value import LazyValueWrapper
Expand Down
26 changes: 13 additions & 13 deletions jedi/inference/compiled/subprocess/__init__.py
Expand Up @@ -29,19 +29,19 @@
PICKLE_PROTOCOL = 4


class _GeneralizedPopen(subprocess.Popen):
def __init__(self, *args, **kwargs):
if os.name == 'nt':
try:
# Was introduced in Python 3.7.
CREATE_NO_WINDOW = subprocess.CREATE_NO_WINDOW
except AttributeError:
CREATE_NO_WINDOW = 0x08000000
kwargs['creationflags'] = CREATE_NO_WINDOW
# The child process doesn't need file descriptors except 0, 1, 2.
# This is unix only.
kwargs['close_fds'] = 'posix' in sys.builtin_module_names
super().__init__(*args, **kwargs)
def _GeneralizedPopen(*args, **kwargs):
if os.name == 'nt':
try:
# Was introduced in Python 3.7.
CREATE_NO_WINDOW = subprocess.CREATE_NO_WINDOW
except AttributeError:
CREATE_NO_WINDOW = 0x08000000
kwargs['creationflags'] = CREATE_NO_WINDOW
# The child process doesn't need file descriptors except 0, 1, 2.
# This is unix only.
kwargs['close_fds'] = 'posix' in sys.builtin_module_names

return subprocess.Popen(*args, **kwargs)


def _enqueue_output(out, queue_):
Expand Down
3 changes: 2 additions & 1 deletion jedi/inference/compiled/subprocess/__main__.py
@@ -1,5 +1,6 @@
import os
import sys
from importlib.abc import MetaPathFinder
from importlib.machinery import PathFinder

# Remove the first entry, because it's simply a directory entry that equals
Expand All @@ -16,7 +17,7 @@ def _get_paths():
return {'jedi': _jedi_path, 'parso': _parso_path}


class _ExactImporter:
class _ExactImporter(MetaPathFinder):
def __init__(self, path_dct):
self._path_dct = path_dct

Expand Down
2 changes: 1 addition & 1 deletion jedi/inference/docstrings.py
Expand Up @@ -50,7 +50,7 @@ def _get_numpy_doc_string_cls():
global _numpy_doc_string_cache
if isinstance(_numpy_doc_string_cache, (ImportError, SyntaxError)):
raise _numpy_doc_string_cache
from numpydoc.docscrape import NumpyDocString
from numpydoc.docscrape import NumpyDocString # type: ignore[import]
_numpy_doc_string_cache = NumpyDocString
return _numpy_doc_string_cache

Expand Down
7 changes: 5 additions & 2 deletions jedi/inference/filters.py
Expand Up @@ -3,18 +3,21 @@
are needed for name resolution.
"""
from abc import abstractmethod
from typing import List, MutableMapping, Type
import weakref

from parso.tree import search_ancestor
from parso.python.tree import Name, UsedNamesMapping

from jedi.inference import flow_analysis
from jedi.inference.base_value import ValueSet, ValueWrapper, \
LazyValueWrapper
from jedi.parser_utils import get_cached_parent_scope
from jedi.inference.utils import to_list
from jedi.inference.names import TreeNameDefinition, ParamName, \
AnonymousParamName, AbstractNameDefinition
AnonymousParamName, AbstractNameDefinition, NameWrapper

_definition_name_cache: MutableMapping[UsedNamesMapping, List[Name]]
_definition_name_cache = weakref.WeakKeyDictionary()


Expand All @@ -36,7 +39,7 @@ def values(self):


class FilterWrapper:
name_wrapper_class = None
name_wrapper_class: Type[NameWrapper]

def __init__(self, wrapped_filter):
self._wrapped_filter = wrapped_filter
Expand Down
6 changes: 4 additions & 2 deletions jedi/inference/flow_analysis.py
@@ -1,12 +1,14 @@
from typing import Dict, Optional

from jedi.parser_utils import get_flow_branch_keyword, is_scope, get_parent_scope
from jedi.inference.recursion import execution_allowed
from jedi.inference.helpers import is_big_annoying_library


class Status:
lookup_table = {}
lookup_table: Dict[Optional[bool], 'Status'] = {}

def __init__(self, value, name):
def __init__(self, value: Optional[bool], name: str) -> None:
self._value = value
self._name = name
Status.lookup_table[value] = self
Expand Down
3 changes: 2 additions & 1 deletion jedi/inference/gradual/typeshed.py
Expand Up @@ -2,6 +2,7 @@
import re
from functools import wraps
from collections import namedtuple
from typing import Dict, Mapping, Tuple
from pathlib import Path

from jedi import settings
Expand Down Expand Up @@ -74,7 +75,7 @@ def _get_typeshed_directories(version_info):
yield PathInfo(str(base_path.joinpath(check_version)), is_third_party)


_version_cache = {}
_version_cache: Dict[Tuple[int, int], Mapping[str, PathInfo]] = {}


def _cache_stub_file_map(version_info):
Expand Down
1 change: 0 additions & 1 deletion jedi/inference/gradual/utils.py
@@ -1,4 +1,3 @@
import os
from pathlib import Path

from jedi.inference.gradual.typeshed import TYPESHED_PATH, create_stub_module
Expand Down
5 changes: 3 additions & 2 deletions jedi/inference/names.py
@@ -1,5 +1,6 @@
from abc import abstractmethod
from inspect import Parameter
from typing import Optional, Tuple

from parso.tree import search_ancestor

Expand All @@ -24,8 +25,8 @@ def _merge_name_docs(names):


class AbstractNameDefinition:
start_pos = None
string_name = None
start_pos: Optional[Tuple[int, int]] = None
string_name: str
parent_context = None
tree_name = None
is_value_name = True
Expand Down
3 changes: 2 additions & 1 deletion jedi/inference/references.py
Expand Up @@ -5,7 +5,8 @@

from jedi.debug import dbg
from jedi.file_io import KnownContentFileIO
from jedi.inference.imports import SubModuleName, load_module_from_path
from jedi.inference.names import SubModuleName
from jedi.inference.imports import load_module_from_path
from jedi.inference.filters import ParserTreeFilter
from jedi.inference.gradual.conversion import convert_names

Expand Down
4 changes: 2 additions & 2 deletions jedi/inference/sys_path.py
Expand Up @@ -14,8 +14,8 @@
_BUILDOUT_PATH_INSERTION_LIMIT = 10


def _abs_path(module_context, path: str):
path = Path(path)
def _abs_path(module_context, str_path: str):
path = Path(str_path)
if path.is_absolute():
return path

Expand Down
3 changes: 3 additions & 0 deletions jedi/inference/value/__init__.py
@@ -1,3 +1,6 @@
# Re-export symbols for wider use. We configure mypy and flake8 to be aware that
# this file does this.

from jedi.inference.value.module import ModuleValue
from jedi.inference.value.klass import ClassValue
from jedi.inference.value.function import FunctionValue, \
Expand Down
2 changes: 1 addition & 1 deletion jedi/inference/value/instance.py
@@ -1,6 +1,6 @@
from abc import abstractproperty

from parso.python.tree import search_ancestor
from parso.tree import search_ancestor

from jedi import debug
from jedi import settings
Expand Down

0 comments on commit 52bbb3e

Please sign in to comment.