Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set python_version and fix uppercase builtins #1577

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ disallow_untyped_defs = true
disallow_incomplete_defs = true
show_error_codes = false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that show_error_codes = false also exists here as legacy, and should be fixed eventually.

You can move it below the # TODO comment you have added.

disable_error_code = empty-body
python_version = 3.11
Copy link
Collaborator

@intgr intgr Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python_version also determines the value of sys.version_info used when interpreting typeshed stubs (for conditionals such as https://github.com/python/typeshed/blob/main/stdlib/typing.pyi#L93-L113)

So if we override this setting, we lose test coverage with different variations of typeshed definitions, and would no longer catch subtle compatibility issues in CI.

I very much prefer the modern formatting of types in error messages, but still I would err on the side of caution and prioritize test coverage here.

I wish mypy had a way to override force_uppercase_builtins in the opposite direction, e.g. force_lowercase_builtins = true. Maybe they would be willing to entertain such a PR if someone is interested doing the work. And same for force_union_syntax.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree - we cannot touch python_version :(

# TODO: update our output assertions to match a new syntax
force_uppercase_builtins = true
force_union_syntax = true


Expand Down
4 changes: 2 additions & 2 deletions tests/typecheck/contrib/admin/test_options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@

class A(admin.ModelAdmin):
formfield_overrides = {
"not a field": { # E: Dict entry 0 has incompatible type "str": "Dict[str, Any]"; expected "Type[Field[Any, Any]]": "Mapping[str, Any]"
"not a field": { # E: Dict entry 0 has incompatible type "str": "dict[str, Any]"; expected "type[Field[Any, Any]]": "Mapping[str, Any]"
"widget": Textarea
}
}
Expand All @@ -150,4 +150,4 @@
pass

class A(ModelAdmin[TestModel]):
model = int # E: Incompatible types in assignment (expression has type "Type[int]", base class "ModelAdmin" defined the type as "Type[TestModel]")
model = int # E: Incompatible types in assignment (expression has type "type[int]", base class "ModelAdmin" defined the type as "type[TestModel]")
4 changes: 2 additions & 2 deletions tests/typecheck/contrib/postgres/test_fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from typing import List
from myapp.models import MyModel
array_val: List[int] = [1]
MyModel(array=array_val) # E: Incompatible type for "array" of "MyModel" (got "List[int]", expected "Union[Sequence[str], Combinable]")
MyModel(array=array_val) # E: Incompatible type for "array" of "MyModel" (got "list[int]", expected "Union[Sequence[str], Combinable]")
non_init = MyModel()
non_init.array = array_val # E: Incompatible types in assignment (expression has type "List[int]", variable has type "Union[Sequence[str], Combinable]")
non_init.array = array_val # E: Incompatible types in assignment (expression has type "list[int]", variable has type "Union[Sequence[str], Combinable]")
installed_apps:
- myapp
files:
Expand Down
2 changes: 1 addition & 1 deletion tests/typecheck/contrib/sitemaps/test_generic_sitemap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
main:26: error: Argument 1 of "location" is incompatible with supertype "Sitemap"; supertype defines the argument type as "Offer"
main:26: note: This violates the Liskov substitution principle
main:26: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
main:40: error: Argument 1 to "GenericSitemap" has incompatible type "Dict[str, List[int]]"; expected "Mapping[str, Union[datetime, _QuerySet[Offer, Offer], str]]"
main:40: error: Argument 1 to "GenericSitemap" has incompatible type "dict[str, list[int]]"; expected "Mapping[str, Union[datetime, _QuerySet[Offer, Offer], str]]"

installed_apps:
- myapp
Expand Down
2 changes: 1 addition & 1 deletion tests/typecheck/db/migrations/test_operations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@

RunSQL(sql=["SOME SQLS", ("SOME SQLS %s, %s", ["PARAM", "ANOTHER PARAM"])], reverse_sql=["SOME SQLS", ("SOME SQLS %s, %s", ["PARAM", "ANOTHER PARAM"])])

RunSQL(sql=("SOME SQL", {})) # E: Argument "sql" to "RunSQL" has incompatible type "Tuple[str, Dict[<nothing>, <nothing>]]"; expected "Union[str, Union[List[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[str], Tuple[str, ...], Tuple[]], None]]]], Tuple[Union[str, Tuple[str, Union[Dict[str, Any], Union[List[str], Tuple[str, ...], Tuple[]], None]]], ...], Tuple[]]]"
RunSQL(sql=("SOME SQL", {})) # E: Argument "sql" to "RunSQL" has incompatible type "tuple[str, dict[<nothing>, <nothing>]]"; expected "Union[str, Union[list[Union[str, tuple[str, Union[dict[str, Any], Union[list[str], tuple[str, ...], tuple[]], None]]]], tuple[Union[str, tuple[str, Union[dict[str, Any], Union[list[str], tuple[str, ...], tuple[]], None]]], ...], tuple[]]]"
RunSQL(sql=["SOME SQLS", ("SOME SQLS %s, %s", [object(), "ANOTHER PARAM"])]) # E: List item 0 has incompatible type "object"; expected "str"
2 changes: 1 addition & 1 deletion tests/typecheck/db/models/test_constraints.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)
regex: true
out: |
main:4: error: No overload variant of "UniqueConstraint" matches argument types "Lower", "List\[str\]", "str"
main:4: error: No overload variant of "UniqueConstraint" matches argument types "Lower", "list\[str\]", "str"
main:4: note: Possible overload variants:
main:4: note: .*
main:4: note: .*
4 changes: 2 additions & 2 deletions tests/typecheck/db/models/test_query.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
class NonTupleValuesListIterable(ValuesListIterable[int]):
pass
out: |
main:10: error: Type argument "int" of "ValuesListIterable" must be a subtype of "Tuple[Any, ...]"
main:10: error: Type argument "int" of "ValuesListIterable" must be a subtype of "tuple[Any, ...]"
- case: django_db_models_query_module_has_NamedValuesListIterable
main: |
from django.db.models.query import NamedValuesListIterable
Expand All @@ -46,4 +46,4 @@
QuerySet._iterable_class = ModelIterable
QuerySet._iterable_class = int
out: |
main:5: error: Incompatible types in assignment (expression has type "Type[int]", variable has type "Type[BaseIterable[Any]]")
main:5: error: Incompatible types in assignment (expression has type "type[int]", variable has type "type[BaseIterable[Any]]")
6 changes: 3 additions & 3 deletions tests/typecheck/fields/test_related.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
reveal_type(book.publisher_id) # N: Revealed type is "uuid.UUID"
book.publisher_id = '821850bb-c105-426f-b340-3974419d00ca'
book.publisher_id = UUID('821850bb-c105-426f-b340-3974419d00ca')
book.publisher_id = [1] # E: Incompatible types in assignment (expression has type "List[int]", variable has type "Union[str, UUID]")
book.publisher_id = [1] # E: Incompatible types in assignment (expression has type "list[int]", variable has type "Union[str, UUID]")
book.publisher_id = Publisher() # E: Incompatible types in assignment (expression has type "Publisher", variable has type "Union[str, UUID]")
installed_apps:
- myapp
Expand Down Expand Up @@ -391,9 +391,9 @@

reveal_type(Book2().publisher_id) # N: Revealed type is "builtins.int"
Book2(publisher_id=1)
Book2(publisher_id=[]) # E: Incompatible type for "publisher_id" of "Book2" (got "List[Any]", expected "Union[float, int, str, Combinable]")
Book2(publisher_id=[]) # E: Incompatible type for "publisher_id" of "Book2" (got "list[Any]", expected "Union[float, int, str, Combinable]")
Book2.objects.create(publisher_id=1)
Book2.objects.create(publisher_id=[]) # E: Incompatible type for "publisher_id" of "Book2" (got "List[Any]", expected "Union[float, int, str, Combinable]")
Book2.objects.create(publisher_id=[]) # E: Incompatible type for "publisher_id" of "Book2" (got "list[Any]", expected "Union[float, int, str, Combinable]")
installed_apps:
- myapp
files:
Expand Down
4 changes: 2 additions & 2 deletions tests/typecheck/managers/querysets/test_annotate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@
from django.db.models.expressions import F

values_list_double_annotate = Blog.objects.annotate(foo=F('id')).annotate(bar=F('id')).values_list('foo', 'bar').get()
reveal_type(values_list_double_annotate) # N: Revealed type is "Tuple[Any, Any]"
reveal_type(values_list_double_annotate) # N: Revealed type is "tuple[Any, Any]"

values_list_named = Blog.objects.annotate(foo=F('id'), bar=F('isad')).values_list('foo', 'text', named=True).get()
# We have to assume we don't know any of the tuple member types.
reveal_type(values_list_named) # N: Revealed type is "Tuple[Any, builtins.str, fallback=main.Row]"
reveal_type(values_list_named) # N: Revealed type is "tuple[Any, builtins.str, fallback=main.Row]"
values_list_named.unknown # E: "Row" has no attribute "unknown"
reveal_type(values_list_named.foo) # N: Revealed type is "Any"
reveal_type(values_list_named.text) # N: Revealed type is "builtins.str"
Expand Down
6 changes: 3 additions & 3 deletions tests/typecheck/managers/querysets/test_basic_methods.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
reveal_type(qs[0]) # N: Revealed type is "myapp.models.Blog"
reveal_type(qs[:9]) # N: Revealed type is "django.db.models.query._QuerySet[myapp.models.Blog, myapp.models.Blog]"
reveal_type(qs.create()) # N: Revealed type is "myapp.models.Blog"
reveal_type(qs.get_or_create()) # N: Revealed type is "Tuple[myapp.models.Blog, builtins.bool]"
reveal_type(qs.get_or_create()) # N: Revealed type is "tuple[myapp.models.Blog, builtins.bool]"
reveal_type(qs.exists()) # N: Revealed type is "builtins.bool"
reveal_type(qs.none()) # N: Revealed type is "django.db.models.query._QuerySet[myapp.models.Blog, myapp.models.Blog]"
reveal_type(qs.update_or_create()) # N: Revealed type is "Tuple[myapp.models.Blog, builtins.bool]"
reveal_type(qs.update_or_create()) # N: Revealed type is "tuple[myapp.models.Blog, builtins.bool]"
reveal_type(qs.explain()) # N: Revealed type is "builtins.str"
reveal_type(qs.raw(qs.explain())) # N: Revealed type is "django.db.models.query.RawQuerySet[Any]"
# .dates / .datetimes
Expand All @@ -32,7 +32,7 @@
reveal_type(qs.in_bulk()) # N: Revealed type is "builtins.dict[Any, myapp.models.Blog]"
reveal_type(qs.bulk_update(list(qs), fields=["created_at"])) # N: Revealed type is "builtins.int"
reveal_type(qs.bulk_create([])) # N: Revealed type is "builtins.list[myapp.models.Blog]"
reveal_type(qs.delete()) # N: Revealed type is "Tuple[builtins.int, builtins.dict[builtins.str, builtins.int]]"
reveal_type(qs.delete()) # N: Revealed type is "tuple[builtins.int, builtins.dict[builtins.str, builtins.int]]"
installed_apps:
- myapp
files:
Expand Down
40 changes: 20 additions & 20 deletions tests/typecheck/managers/querysets/test_values_list.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
- case: values_list_simple_field_returns_queryset_of_tuples
main: |
from myapp.models import MyUser
reveal_type(MyUser.objects.values_list('name').get()) # N: Revealed type is "Tuple[builtins.str]"
reveal_type(MyUser.objects.values_list('id', 'name').get()) # N: Revealed type is "Tuple[builtins.int, builtins.str]"
reveal_type(MyUser.objects.values_list('name').get()) # N: Revealed type is "tuple[builtins.str]"
reveal_type(MyUser.objects.values_list('id', 'name').get()) # N: Revealed type is "tuple[builtins.int, builtins.str]"

values_tuple = MyUser.objects.values_list('name', 'age').get()
reveal_type(values_tuple[0]) # N: Revealed type is "builtins.str"
reveal_type(values_tuple[1]) # N: Revealed type is "builtins.int"

# no fields specified return all fields
all_values_tuple = MyUser.objects.values_list().get()
reveal_type(all_values_tuple) # N: Revealed type is "Tuple[builtins.int, builtins.str, builtins.int]"
reveal_type(all_values_tuple) # N: Revealed type is "tuple[builtins.int, builtins.str, builtins.int]"

# pk as field
pk_values = MyUser.objects.values_list('pk').get()
reveal_type(pk_values) # N: # N: Revealed type is "Tuple[builtins.int]"
reveal_type(pk_values) # N: # N: Revealed type is "tuple[builtins.int]"
installed_apps:
- myapp
files:
Expand All @@ -32,7 +32,7 @@

# Values tuples can have the same field repeated
values_tuple = MyUser.objects.values_list('name', 'age', 'name').get()
reveal_type(values_tuple) # N: Revealed type is "Tuple[builtins.str, builtins.int, builtins.str]"
reveal_type(values_tuple) # N: Revealed type is "tuple[builtins.str, builtins.int, builtins.str]"
reveal_type(values_tuple[0]) # N: Revealed type is "builtins.str"
reveal_type(values_tuple[1]) # N: Revealed type is "builtins.int"
reveal_type(values_tuple[2]) # N: Revealed type is "builtins.str"
Expand All @@ -52,12 +52,12 @@
from myapp.models import MyUser
from django.db.models.functions import Length
query = MyUser.objects.values_list('name')
reveal_type(query.order_by("name").get()) # N: Revealed type is "Tuple[builtins.str]"
reveal_type(query.distinct("name").get()) # N: Revealed type is "Tuple[builtins.str]"
reveal_type(query.distinct().get()) # N: Revealed type is "Tuple[builtins.str]"
reveal_type(query.all().get()) # N: Revealed type is "Tuple[builtins.str]"
reveal_type(query.filter(age__gt=16).get()) # N: Revealed type is "Tuple[builtins.str]"
reveal_type(query.exclude(age__lte=16).get()) # N: Revealed type is "Tuple[builtins.str]"
reveal_type(query.order_by("name").get()) # N: Revealed type is "tuple[builtins.str]"
reveal_type(query.distinct("name").get()) # N: Revealed type is "tuple[builtins.str]"
reveal_type(query.distinct().get()) # N: Revealed type is "tuple[builtins.str]"
reveal_type(query.all().get()) # N: Revealed type is "tuple[builtins.str]"
reveal_type(query.filter(age__gt=16).get()) # N: Revealed type is "tuple[builtins.str]"
reveal_type(query.exclude(age__lte=16).get()) # N: Revealed type is "tuple[builtins.str]"
reveal_type(query.annotate(name_length=Length("name")).get()) # N: Revealed type is "builtins.tuple[Any, ...]"
installed_apps:
- myapp
Expand All @@ -80,7 +80,7 @@
reveal_type(values_tuple[3]) # N: Revealed type is "builtins.str"

reverse_fields_list = Blog.objects.values_list('post__text').get()
reveal_type(reverse_fields_list) # N: Revealed type is "Tuple[builtins.str]"
reveal_type(reverse_fields_list) # N: Revealed type is "tuple[builtins.str]"
installed_apps:
- myapp
files:
Expand Down Expand Up @@ -126,7 +126,7 @@
main: |
from myapp.models import MyUser
values_named_tuple = MyUser.objects.values_list('name', 'age', named=True).get()
reveal_type(values_named_tuple) # N: Revealed type is "Tuple[builtins.str, builtins.int, fallback=main.Row]"
reveal_type(values_named_tuple) # N: Revealed type is "tuple[builtins.str, builtins.int, fallback=main.Row]"
reveal_type(values_named_tuple.name) # N: Revealed type is "builtins.str"
reveal_type(values_named_tuple.age) # N: Revealed type is "builtins.int"

Expand All @@ -139,13 +139,13 @@

# pk as field
pk_values = MyUser.objects.values_list('pk', named=True).get()
reveal_type(pk_values) # N: Revealed type is "Tuple[builtins.int, fallback=main.Row2]"
reveal_type(pk_values) # N: Revealed type is "tuple[builtins.int, fallback=main.Row2]"
reveal_type(pk_values.pk) # N: # N: Revealed type is "builtins.int"

# values_list(named=True) inside function
def func() -> None:
from myapp.models import MyUser
reveal_type(MyUser.objects.values_list('name', named=True).get()) # N: Revealed type is "Tuple[builtins.str, fallback=main.Row3]"
reveal_type(MyUser.objects.values_list('name', named=True).get()) # N: Revealed type is "tuple[builtins.str, fallback=main.Row3]"
installed_apps:
- myapp
files:
Expand Down Expand Up @@ -189,7 +189,7 @@
main:3: note: Revealed type is "Any"
main:4: error: Cannot resolve keyword 'unknown' into field. Choices are: id, publisher, publisher_id
main:4: note: Revealed type is "Any"
main:5: note: Revealed type is "Tuple[Any]"
main:5: note: Revealed type is "tuple[Any]"
installed_apps:
- myapp
files:
Expand Down Expand Up @@ -257,7 +257,7 @@
from myapp.models import TransactionQuerySet
reveal_type(TransactionQuerySet()) # N: Revealed type is "myapp.models.TransactionQuerySet"
reveal_type(TransactionQuerySet().values()) # N: Revealed type is "django.db.models.query._QuerySet[myapp.models.Transaction, TypedDict({'id': builtins.int, 'total': builtins.int})]"
reveal_type(TransactionQuerySet().values_list()) # N: Revealed type is "django.db.models.query._QuerySet[myapp.models.Transaction, Tuple[builtins.int, builtins.int]]"
reveal_type(TransactionQuerySet().values_list()) # N: Revealed type is "django.db.models.query._QuerySet[myapp.models.Transaction, tuple[builtins.int, builtins.int]]"
installed_apps:
- myapp
files:
Expand All @@ -273,8 +273,8 @@
- case: values_list_of_many_to_many_field
main: |
from myapp.models import Author, Book
reveal_type(Book.objects.values_list('authors')) # N: Revealed type is "django.db.models.query._QuerySet[myapp.models.Book, Tuple[builtins.int]]"
reveal_type(Author.objects.values_list('books')) # N: Revealed type is "django.db.models.query._QuerySet[myapp.models.Author, Tuple[builtins.int]]"
reveal_type(Book.objects.values_list('authors')) # N: Revealed type is "django.db.models.query._QuerySet[myapp.models.Book, tuple[builtins.int]]"
reveal_type(Author.objects.values_list('books')) # N: Revealed type is "django.db.models.query._QuerySet[myapp.models.Author, tuple[builtins.int]]"
installed_apps:
- myapp
files:
Expand All @@ -290,7 +290,7 @@
main: |
from myapp.models import Blog
values = Blog.objects.values_list('text').get()
reveal_type(values) # N: Revealed type is "Tuple[builtins.str]"
reveal_type(values) # N: Revealed type is "tuple[builtins.str]"
installed_apps:
- myapp
files:
Expand Down
2 changes: 1 addition & 1 deletion tests/typecheck/models/test_abstract.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
abstract = True

def lock(self) -> None:
reveal_type(type(self).objects.values_list("pk")) # N: Revealed type is "django.db.models.query._QuerySet[myapp.models.BaseModel, Tuple[Any]]"
reveal_type(type(self).objects.values_list("pk")) # N: Revealed type is "django.db.models.query._QuerySet[myapp.models.BaseModel, tuple[Any]]"

class MyModel(BaseModel):
field = models.IntegerField()
2 changes: 1 addition & 1 deletion tests/typecheck/models/test_create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
main: |
from myapp.models import User
User.objects.create(pk=1, name='Max', age=10)
User.objects.create(age=[]) # E: Incompatible type for "age" of "User" (got "List[Any]", expected "Union[float, int, str, Combinable]")
User.objects.create(age=[]) # E: Incompatible type for "age" of "User" (got "list[Any]", expected "Union[float, int, str, Combinable]")
installed_apps:
- myapp
files:
Expand Down
Loading