namespace.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from pathlib import Path
  2. from typing import Optional
  3. from jedi.inference.cache import inference_state_method_cache
  4. from jedi.inference.filters import DictFilter
  5. from jedi.inference.names import ValueNameMixin, AbstractNameDefinition
  6. from jedi.inference.base_value import Value
  7. from jedi.inference.value.module import SubModuleDictMixin
  8. from jedi.inference.context import NamespaceContext
  9. class ImplicitNSName(ValueNameMixin, AbstractNameDefinition):
  10. """
  11. Accessing names for implicit namespace packages should infer to nothing.
  12. This object will prevent Jedi from raising exceptions
  13. """
  14. def __init__(self, implicit_ns_value, string_name):
  15. self._value = implicit_ns_value
  16. self.string_name = string_name
  17. class ImplicitNamespaceValue(Value, SubModuleDictMixin):
  18. """
  19. Provides support for implicit namespace packages
  20. """
  21. api_type = 'namespace'
  22. parent_context = None
  23. def __init__(self, inference_state, string_names, paths):
  24. super().__init__(inference_state, parent_context=None)
  25. self.inference_state = inference_state
  26. self.string_names = string_names
  27. self._paths = paths
  28. def get_filters(self, origin_scope=None):
  29. yield DictFilter(self.sub_modules_dict())
  30. def get_qualified_names(self):
  31. return ()
  32. @property # type: ignore[misc]
  33. @inference_state_method_cache()
  34. def name(self):
  35. string_name = self.py__package__()[-1]
  36. return ImplicitNSName(self, string_name)
  37. def py__file__(self) -> Optional[Path]:
  38. return None
  39. def py__package__(self):
  40. """Return the fullname
  41. """
  42. return self.string_names
  43. def py__path__(self):
  44. return self._paths
  45. def py__name__(self):
  46. return '.'.join(self.string_names)
  47. def is_namespace(self):
  48. return True
  49. def is_stub(self):
  50. return False
  51. def is_package(self):
  52. return True
  53. def as_context(self):
  54. return NamespaceContext(self)
  55. def __repr__(self):
  56. return '<%s: %s>' % (self.__class__.__name__, self.py__name__())