Merge branch 'master' into fix-nested-tuple-argument

This commit is contained in:
Peter Law
2020-04-26 13:56:14 +01:00
29 changed files with 512 additions and 57 deletions

168
test/completion/django.py Normal file
View File

@@ -0,0 +1,168 @@
import datetime
import decimal
from django.db import models
from django.contrib.auth.models import User
class Tag(models.Model):
tag_name = models.CharField()
class Category(models.Model):
category_name = models.CharField()
class BusinessModel(models.Model):
category_fk = models.ForeignKey(Category)
category_fk2 = models.ForeignKey('Category')
category_fk3 = models.ForeignKey(1)
category_fk4 = models.ForeignKey('models')
category_fk5 = models.ForeignKey()
integer_field = models.IntegerField()
big_integer_field = models.BigIntegerField()
positive_integer_field = models.PositiveIntegerField()
small_integer_field = models.SmallIntegerField()
char_field = models.CharField()
text_field = models.TextField()
email_field = models.EmailField()
float_field = models.FloatField()
binary_field = models.BinaryField()
boolean_field = models.BooleanField()
decimal_field = models.DecimalField()
time_field = models.TimeField()
duration_field = models.DurationField()
date_field = models.DateField()
date_time_field = models.DateTimeField()
tags_m2m = models.ManyToManyField(Tag)
unidentifiable = NOT_FOUND
# -----------------
# Model attribute inference
# -----------------
model_instance = BusinessModel()
#? int()
model_instance.integer_field
#? int()
model_instance.big_integer_field
#? int()
model_instance.positive_integer_field
#? int()
model_instance.small_integer_field
#? str()
model_instance.char_field
#? str()
model_instance.text_field
#? str()
model_instance.email_field
#? float()
model_instance.float_field
#? bytes()
model_instance.binary_field
#? bool()
model_instance.boolean_field
#? decimal.Decimal()
model_instance.decimal_field
#? datetime.time()
model_instance.time_field
#? datetime.timedelta()
model_instance.duration_field
#? datetime.date()
model_instance.date_field
#? datetime.datetime()
model_instance.date_time_field
#! ['category_fk = models.ForeignKey(Category)']
model_instance.category_fk
#! ['category_name = models.CharField()']
model_instance.category_fk.category_name
#? Category()
model_instance.category_fk
#? str()
model_instance.category_fk.category_name
#? Category()
model_instance.category_fk2
#? str()
model_instance.category_fk2.category_name
#?
model_instance.category_fk3
#?
model_instance.category_fk4
#?
model_instance.category_fk5
#? models.manager.RelatedManager()
model_instance.tags_m2m
#? Tag()
model_instance.tags_m2m.get()
#? ['add']
model_instance.tags_m2m.add
#?
model_instance.unidentifiable
#! ['unidentifiable = NOT_FOUND']
model_instance.unidentifiable
# -----------------
# Queries
# -----------------
#? models.query.QuerySet.filter
model_instance.objects.filter
#? BusinessModel() None
model_instance.objects.filter().first()
#? str()
model_instance.objects.get().char_field
#? int()
model_instance.objects.update(x='')
#? BusinessModel()
model_instance.objects.create()
# -----------------
# Inheritance
# -----------------
class Inherited(BusinessModel):
text_field = models.IntegerField()
new_field = models.FloatField()
inherited = Inherited()
#? int()
inherited.text_field
#? str()
inherited.char_field
#? float()
inherited.new_field
#? str()
inherited.category_fk2.category_name
#? str()
inherited.objects.get().char_field
#? int()
inherited.objects.get().text_field
#? float()
inherited.objects.get().new_field
# -----------------
# Django Auth
# -----------------
#? str()
User().email
#? str()
User.objects.get().email
# -----------------
# values & values_list (dave is too lazy to implement it)
# -----------------
#?
model_instance.objects.values_list('char_field')[0]
#? dict()
model_instance.objects.values('char_field')[0]
#?
model_instance.objects.values('char_field')[0]['char_field']

View File

@@ -207,40 +207,36 @@ for a in list_func_t_to_list_t(12):
a
# The following are all actually wrong, however we're mainly testing here that
# we don't error when processing invalid values, rather than that we get the
# right output.
x0 = list_func_t_to_list_t(["abc"])[0]
#? str()
#?
x0
x2 = list_func_t_to_list_t([tpl])[0]
#? tuple()
#?
x2
x3 = list_func_t_to_list_t([tpl_typed])[0]
#? tuple()
#?
x3
x4 = list_func_t_to_list_t([collection])[0]
#? dict()
#?
x4
x5 = list_func_t_to_list_t([collection_typed])[0]
#? dict()
#?
x5
x6 = list_func_t_to_list_t([custom_generic])[0]
#? CustomGeneric()
#?
x6
x7 = list_func_t_to_list_t([plain_instance])[0]
#? PlainClass()
#?
x7
for a in list_func_t_to_list_t([12]):
#? int()
#?
a

View File

@@ -60,6 +60,27 @@ for b in list_type_t_to_list_t(list_of_int_type):
b
# Test construction of nested generic tuple return parameters
def list_t_to_list_tuple_t(the_list: List[T]) -> List[Tuple[T]]:
return [(x,) for x in the_list]
x1t = list_t_to_list_tuple_t(list_of_ints)[0][0]
#? int()
x1t
for c1 in list_t_to_list_tuple_t(list_of_ints):
#? int()
c1[0]
for c2, in list_t_to_list_tuple_t(list_of_ints):
#? int()
c2
# Test handling of nested tuple input parameters
def list_tuple_t_to_tuple_list_t(the_list: List[Tuple[T]]) -> Tuple[List[T], ...]:
return tuple(list(x) for x in the_list)
@@ -82,11 +103,12 @@ for b in list_tuple_t_elipsis_to_tuple_list_t(list_of_int_tuple_elipsis):
b[0]
def foo(x: T) -> T:
# Test handling of nested callables
def foo(x: int) -> int:
return x
list_of_funcs = [foo] # type: List[Callable[[T], T]]
list_of_funcs = [foo] # type: List[Callable[[int], int]]
def list_func_t_to_list_func_type_t(the_list: List[Callable[[T], T]]) -> List[Callable[[Type[T]], T]]:
def adapt(func: Callable[[T], T]) -> Callable[[Type[T]], T]:
@@ -101,6 +123,21 @@ for b in list_func_t_to_list_func_type_t(list_of_funcs):
b(int)
def bar(*a, **k) -> int:
return len(a) + len(k)
list_of_funcs_2 = [bar] # type: List[Callable[..., int]]
def list_func_t_passthrough(the_list: List[Callable[..., T]]) -> List[Callable[..., T]]:
return the_list
for b in list_func_t_passthrough(list_of_funcs_2):
#? int()
b(None, x="x")
mapping_int_str = {42: 'a'} # type: Dict[int, str]
# Test that mappings (that have more than one parameter) are handled

View File

@@ -283,6 +283,18 @@ def testnewtype2(y):
y
#? []
y.
# The type of a NewType is equivalent to the type of its underlying type.
MyInt = typing.NewType('MyInt', int)
x = type(MyInt)
#? type.mro
x.mro
PlainInt = int
y = type(PlainInt)
#? type.mro
y.mro
# python > 2.7
class TestDefaultDict(typing.DefaultDict[str, int]):

View File

@@ -103,3 +103,15 @@ while True:
bar = bar # type: bar
#? int()
bar
class Comprehension:
def __init__(self, foo):
self.foo = foo
def update(self):
self.foo = (self.foo,)
#? int() tuple()
Comprehension(1).foo[0]

View File

@@ -1,11 +0,0 @@
#! ['class ObjectDoesNotExist']
from django.core.exceptions import ObjectDoesNotExist
import django
#? ['get_version']
django.get_version
from django.conf import settings
#? ['configured']
settings.configured

View File

@@ -115,3 +115,18 @@ def test_docstring_decorator(goto_or_help_or_infer, skip_python2):
doc = d.docstring()
assert doc == 'FunctionType(*args: Any, **kwargs: Any) -> Any\n\nhello'
@pytest.mark.parametrize('code', ['', '\n', ' '])
def test_empty(Script, code):
assert not Script(code).help(1, 0)
@pytest.mark.parametrize('code', ['f()', '(bar or baz)', 'f[3]'])
def test_no_help_for_operator(Script, code):
assert not Script(code).help()
@pytest.mark.parametrize('code', ['()', '(1,)', '[]', '[1]', 'f[]'])
def test_help_for_operator(Script, code):
assert Script(code).help()

View File

@@ -20,6 +20,12 @@ def test_django_default_project(Script):
assert script._inference_state.project._django is True
def test_django_default_project_of_file(Script):
project = get_default_project(__file__)
d = os.path.dirname
assert project._path == d(d(d(__file__)))
def test_interpreter_project_path():
# Run from anywhere it should be the cwd.
dir = os.path.join(root_dir, 'test')

View File

@@ -36,7 +36,7 @@ unspecified = %s
""" % (case, sorted(d - a), sorted(a - d))
def test_completion(case, monkeypatch, environment, has_typing):
def test_completion(case, monkeypatch, environment, has_typing, has_django):
skip_reason = case.get_skip_reason(environment)
if skip_reason is not None:
pytest.skip(skip_reason)
@@ -47,6 +47,8 @@ def test_completion(case, monkeypatch, environment, has_typing):
_CONTAINS_TYPING = ('pep0484_typing', 'pep0484_comments', 'pep0526_variables')
if not has_typing and any(x in case.path for x in _CONTAINS_TYPING):
pytest.skip('Needs the typing module installed to run this test.')
if (not has_django or environment.version_info.major == 2) and case.path.endswith('django.py'):
pytest.skip('Needs django to be installed to run this test.')
repo_root = helpers.root_dir
monkeypatch.chdir(os.path.join(repo_root, 'jedi'))
case.run(assert_case_equal, environment)