From aa265a44e14c60538b1efc68e2b142b8061f1797 Mon Sep 17 00:00:00 2001 From: Peter Law Date: Sun, 30 Aug 2020 18:03:03 +0100 Subject: [PATCH] Have all py__file__ methods return a Path --- jedi/inference/compiled/access.py | 6 ++++-- jedi/inference/compiled/value.py | 8 +++----- jedi/inference/context.py | 14 ++++++++------ jedi/inference/value/namespace.py | 5 ++++- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/jedi/inference/compiled/access.py b/jedi/inference/compiled/access.py index 2a674448..91f2da08 100644 --- a/jedi/inference/compiled/access.py +++ b/jedi/inference/compiled/access.py @@ -8,6 +8,8 @@ import warnings import re import builtins import typing +from pathlib import Path +from typing import Optional from jedi.inference.compiled.getattr_static import getattr_static @@ -179,9 +181,9 @@ class DirectObjectAccess: def py__bool__(self): return bool(self._obj) - def py__file__(self): + def py__file__(self) -> Optional[Path]: try: - return self._obj.__file__ + return Path(self._obj.__file__) except AttributeError: return None diff --git a/jedi/inference/compiled/value.py b/jedi/inference/compiled/value.py index 7b61e717..baad4d63 100644 --- a/jedi/inference/compiled/value.py +++ b/jedi/inference/compiled/value.py @@ -5,6 +5,7 @@ import re from functools import partial from inspect import Parameter from pathlib import Path +from typing import Optional from jedi import debug from jedi.inference.utils import to_list @@ -305,11 +306,8 @@ class CompiledModule(CompiledValue): return () return tuple(name.split('.')) - def py__file__(self): - path = self.access_handle.py__file__() - if path is None: - return None - return Path(path) + def py__file__(self) -> Optional[Path]: + return self.access_handle.py__file__() # type: ignore[no-any-return] class CompiledName(AbstractNameDefinition): diff --git a/jedi/inference/context.py b/jedi/inference/context.py index dd5b10da..5bc6b994 100644 --- a/jedi/inference/context.py +++ b/jedi/inference/context.py @@ -1,5 +1,7 @@ from abc import abstractmethod from contextlib import contextmanager +from pathlib import Path +from typing import Optional from parso.tree import search_ancestor from parso.python.tree import Name @@ -307,8 +309,8 @@ class FunctionContext(TreeContextMixin, ValueContext): class ModuleContext(TreeContextMixin, ValueContext): - def py__file__(self): - return self._value.py__file__() + def py__file__(self) -> Optional[Path]: + return self._value.py__file__() # type: ignore[no-any-return] def get_filters(self, until_position=None, origin_scope=None): filters = self._value.get_filters(origin_scope) @@ -355,8 +357,8 @@ class NamespaceContext(TreeContextMixin, ValueContext): def string_names(self): return self._value.string_names - def py__file__(self): - return self._value.py__file__() + def py__file__(self) -> Optional[Path]: + return self._value.py__file__() # type: ignore[no-any-return] class ClassContext(TreeContextMixin, ValueContext): @@ -405,8 +407,8 @@ class CompiledModuleContext(CompiledContext): def string_names(self): return self._value.string_names - def py__file__(self): - return self._value.py__file__() + def py__file__(self) -> Optional[Path]: + return self._value.py__file__() # type: ignore[no-any-return] def _get_global_filters_for_name(context, name_or_none, position): diff --git a/jedi/inference/value/namespace.py b/jedi/inference/value/namespace.py index f9f5d09f..11737cc9 100644 --- a/jedi/inference/value/namespace.py +++ b/jedi/inference/value/namespace.py @@ -1,3 +1,6 @@ +from pathlib import Path +from typing import Optional + from jedi.inference.cache import inference_state_method_cache from jedi.inference.filters import DictFilter from jedi.inference.names import ValueNameMixin, AbstractNameDefinition @@ -41,7 +44,7 @@ class ImplicitNamespaceValue(Value, SubModuleDictMixin): string_name = self.py__package__()[-1] return ImplicitNSName(self, string_name) - def py__file__(self): + def py__file__(self) -> Optional[Path]: return None def py__package__(self):