Have all py__file__ methods return a Path

This commit is contained in:
Peter Law
2020-08-30 18:03:03 +01:00
parent 25a3e31ca8
commit aa265a44e1
4 changed files with 19 additions and 14 deletions

View File

@@ -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

View File

@@ -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):

View File

@@ -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):

View File

@@ -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):