Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurnikov committed Feb 6, 2019
1 parent 2559901 commit d43cb1f
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 30 deletions.
1 change: 1 addition & 0 deletions django-stubs/contrib/auth/base_user.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class AbstractBaseUser(models.Model):
last_login: Optional[models.DateTimeField] = ...
is_active: models.BooleanField = ...
REQUIRED_FIELDS: List[str] = ...
class Meta: ...
def get_username(self) -> str: ...
def clean(self) -> None: ...
def save(self, *args: Any, **kwargs: Any) -> None: ...
Expand Down
2 changes: 2 additions & 0 deletions django-stubs/contrib/auth/decorators.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Any, Callable, List, Optional, Set, Union

from django.contrib.auth import REDIRECT_FIELD_NAME as REDIRECT_FIELD_NAME

def user_passes_test(
test_func: Callable, login_url: Optional[str] = ..., redirect_field_name: str = ...
) -> Callable: ...
Expand Down
25 changes: 24 additions & 1 deletion django-stubs/contrib/messages/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
from .api import get_level as get_level, set_level as set_level
from .api import (
get_level as get_level,
set_level as set_level,
add_message as add_message,
debug as debug,
error as error,
success as success,
get_messages as get_messages,
MessageFailure as MessageFailure,
info as info,
warning as warning,
)

from .constants import (
DEBUG as DEBUG,
DEFAULT_LEVELS as DEFAULT_LEVELS,
DEFAULT_TAGS as DEFAULT_TAGS,
ERROR as ERROR,
INFO as INFO,
SUCCESS as SUCCESS,
WARNING as WARNING,
)

default_app_config: str = ...
1 change: 1 addition & 0 deletions django-stubs/core/validators.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from typing import Any, Dict, List, Optional, Union, Pattern
from uuid import UUID

from django.core.files.base import File
from django.core.exceptions import ValidationError as ValidationError

EMPTY_VALUES: Any

Expand Down
7 changes: 7 additions & 0 deletions django-stubs/db/models/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ from .aggregates import (
)

from .fields import (
FieldDoesNotExist as FieldDoesNotExist,
AutoField as AutoField,
IntegerField as IntegerField,
PositiveIntegerField as PositiveIntegerField,
Expand All @@ -38,13 +39,19 @@ from .fields import (
BinaryField as BinaryField,
DurationField as DurationField,
BigAutoField as BigAutoField,
FileField as FileField,
CommaSeparatedIntegerField as CommaSeparatedIntegerField,
)

from .fields.related import (
ForeignKey as ForeignKey,
OneToOneField as OneToOneField,
ManyToManyField as ManyToManyField,
ForeignObject as ForeignObject,
ManyToManyRel as ManyToManyRel,
ManyToOneRel as ManyToOneRel,
OneToOneRel as OneToOneRel,
ForeignObjectRel as ForeignObjectRel,
)
from .fields.files import ImageField as ImageField, FileField as FileField
from .fields.proxy import OrderWrt as OrderWrt
Expand Down
55 changes: 46 additions & 9 deletions django-stubs/db/models/fields/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from typing import Any, Optional, Tuple, Iterable, Callable, Dict, Union, Type
import decimal

from django.core.files.storage import Storage
from django.db.models import Model
from django.db.models.query_utils import RegisterLookupMixin

from django.db.models.expressions import F, Combinable
from django.core.exceptions import FieldDoesNotExist as FieldDoesNotExist
from django.forms import Widget, Field as FormField
from .mixins import NOT_PROVIDED as NOT_PROVIDED

_Choice = Tuple[Any, str]
_ChoiceNamedGroup = Tuple[str, Iterable[_Choice]]
_ChoiceNamedGroup = Union[Tuple[str, Iterable[_Choice]], Tuple[str, Any]]
_FieldChoices = Iterable[Union[_Choice, _ChoiceNamedGroup]]

_ValidatorCallable = Callable[..., None]
Expand All @@ -20,11 +22,12 @@ class Field(RegisterLookupMixin):
help_text: str
db_table: str
remote_field: Field
max_length: Optional[int]
model: Type[Model]
name: str
def __init__(
self,
verbose_name: Optional[str] = ...,
verbose_name: Optional[Union[str, bytes]] = ...,
name: Optional[str] = ...,
primary_key: bool = ...,
max_length: Optional[int] = ...,
Expand Down Expand Up @@ -52,6 +55,7 @@ class Field(RegisterLookupMixin):
def db_type(self, connection: Any) -> str: ...
def db_parameters(self, connection: Any) -> Dict[str, str]: ...
def get_prep_value(self, value: Any) -> Any: ...
def get_internal_type(self) -> str: ...
def formfield(self, **kwargs) -> FormField: ...
def contribute_to_class(self, cls: Type[Model], name: str, private_only: bool = ...) -> None: ...

Expand All @@ -71,7 +75,7 @@ class FloatField(Field): ...
class DecimalField(Field):
def __init__(
self,
verbose_name: Optional[str] = ...,
verbose_name: Optional[Union[str, bytes]] = ...,
name: Optional[str] = ...,
max_digits: Optional[int] = ...,
decimal_places: Optional[int] = ...,
Expand Down Expand Up @@ -99,7 +103,7 @@ class AutoField(Field):
class CharField(Field):
def __init__(
self,
verbose_name: Optional[str] = ...,
verbose_name: Optional[Union[str, bytes]] = ...,
name: Optional[str] = ...,
primary_key: bool = ...,
max_length: Optional[int] = ...,
Expand Down Expand Up @@ -127,7 +131,7 @@ class CharField(Field):
class SlugField(CharField):
def __init__(
self,
verbose_name: Optional[str] = ...,
verbose_name: Optional[Union[str, bytes]] = ...,
name: Optional[str] = ...,
primary_key: bool = ...,
max_length: Optional[int] = ...,
Expand Down Expand Up @@ -166,7 +170,34 @@ class NullBooleanField(Field):
def __set__(self, instance, value: Optional[bool]) -> None: ...
def __get__(self, instance, owner) -> Optional[bool]: ...

class FileField(Field): ...
class FileField(Field):
def __init__(
self,
verbose_name: Optional[Union[str, bytes]] = ...,
name: Optional[str] = ...,
upload_to: str = ...,
storage: Optional[Storage] = ...,
primary_key: bool = ...,
max_length: Optional[int] = ...,
unique: bool = ...,
blank: bool = ...,
null: bool = ...,
db_index: bool = ...,
default: Any = ...,
editable: bool = ...,
auto_created: bool = ...,
serialize: bool = ...,
unique_for_date: Optional[str] = ...,
unique_for_month: Optional[str] = ...,
unique_for_year: Optional[str] = ...,
choices: Optional[_FieldChoices] = ...,
help_text: str = ...,
db_column: Optional[str] = ...,
db_tablespace: Optional[str] = ...,
validators: Iterable[_ValidatorCallable] = ...,
error_messages: Optional[_ErrorMessagesToOverride] = ...,
): ...

class IPAddressField(Field): ...

class GenericIPAddressField(Field):
Expand Down Expand Up @@ -201,7 +232,7 @@ class DateTimeCheckMixin: ...
class DateField(DateTimeCheckMixin, Field):
def __init__(
self,
verbose_name: Optional[str] = ...,
verbose_name: Optional[Union[str, bytes]] = ...,
name: Optional[str] = ...,
auto_now: bool = ...,
auto_now_add: bool = ...,
Expand All @@ -226,7 +257,7 @@ class DateField(DateTimeCheckMixin, Field):
class TimeField(DateTimeCheckMixin, Field):
def __init__(
self,
verbose_name: Optional[str] = ...,
verbose_name: Optional[Union[str, bytes]] = ...,
name: Optional[str] = ...,
auto_now: bool = ...,
auto_now_add: bool = ...,
Expand All @@ -251,9 +282,14 @@ class DateTimeField(DateField): ...
class UUIDField(Field): ...

class FilePathField(Field):
path: str = ...
match: Optional[Any] = ...
recursive: bool = ...
allow_files: bool = ...
allow_folders: bool = ...
def __init__(
self,
verbose_name: Optional[str] = ...,
verbose_name: Optional[Union[str, bytes]] = ...,
name: Optional[str] = ...,
path: str = ...,
match: Optional[Any] = ...,
Expand Down Expand Up @@ -281,3 +317,4 @@ class FilePathField(Field):
class BinaryField(Field): ...
class DurationField(Field): ...
class BigAutoField(AutoField): ...
class CommaSeparatedIntegerField(CharField): ...
4 changes: 3 additions & 1 deletion django-stubs/db/models/fields/files.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable, List, Optional, Type, Union
from typing import Any, Callable, List, Optional, Type, Union, Tuple

from django.core.checks.messages import Error
from django.core.files.base import File
Expand All @@ -9,6 +9,8 @@ from django.db.models.base import Model
from django.db.models.fields import Field
from django.forms import fields as form_fields

BLANK_CHOICE_DASH: List[Tuple[str, str]] = ...

class FieldFile(File):
instance: Model = ...
field: FileField = ...
Expand Down
1 change: 1 addition & 0 deletions django-stubs/db/models/fields/related.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ from django.db.models.fields.reverse_related import (
ForeignObjectRel as ForeignObjectRel,
ManyToManyRel as ManyToManyRel,
ManyToOneRel as ManyToOneRel,
OneToOneRel as OneToOneRel,
)
from django.db.models.query_utils import PathInfo, Q

Expand Down
4 changes: 2 additions & 2 deletions django-stubs/db/models/options.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ from django.db.models.fields import Field, mixins, AutoField
PROXY_PARENTS: Any
EMPTY_RELATION_TREE: Any
IMMUTABLE_WARNING: str
DEFAULT_NAMES: Any
DEFAULT_NAMES: Tuple[str, ...]

def normalize_together(
option_together: Any
Expand Down Expand Up @@ -102,7 +102,7 @@ class Options:
def swapped(self) -> Optional[str]: ...
def many_to_many(self) -> ImmutableList: ...
def fields_map(self) -> Dict[str, ForeignObjectRel]: ...
def get_field(self, field_name: Union[Callable, str]) -> Union[Field, mixins.FieldCacheMixin]: ...
def get_field(self, field_name: Union[Callable, str]) -> Field: ...
def get_base_chain(self, model: Type[Model]) -> List[Type[Model]]: ...
def get_parent_list(self) -> List[Type[Model]]: ...
def get_ancestor_link(self, ancestor: Type[Model]) -> Optional[OneToOneField]: ...
Expand Down
2 changes: 2 additions & 0 deletions django-stubs/http/multipartparser.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ class BoundaryIter:
class Parser:
def __init__(self, stream: LazyStream, boundary: bytes) -> None: ...
def __iter__(self) -> Iterator[Tuple[str, Dict[str, Tuple[str, Dict[str, Union[bytes, str]]]], LazyStream]]: ...

def parse_header(line: bytes) -> Tuple[str, Dict[str, Any]]: ...
1 change: 1 addition & 0 deletions django-stubs/template/loader.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Dict, List, Optional, Union
from . import engines as engines

from django.http.request import HttpRequest
from django.template.exceptions import TemplateDoesNotExist as TemplateDoesNotExist
Expand Down
15 changes: 5 additions & 10 deletions django-stubs/utils/encoding.pyi
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
from datetime import date
from typing import Any, Optional, Union

from django.db.models.base import Model
from typing import Any, Optional

class DjangoUnicodeDecodeError(UnicodeDecodeError):
obj: bytes = ...
def __init__(self, obj: bytes, *args: Any) -> None: ...

python_2_unicode_compatible: Any

def smart_text(s: Union[Model, int, str], encoding: str = ..., strings_only: bool = ..., errors: str = ...) -> str: ...
def smart_text(s: Any, encoding: str = ..., strings_only: bool = ..., errors: str = ...) -> str: ...
def is_protected_type(obj: Any) -> bool: ...
def force_text(
s: Optional[Union[bytes, Model, int, str]], encoding: str = ..., strings_only: bool = ..., errors: str = ...
) -> Optional[Union[int, str]]: ...
def smart_bytes(s: Union[int, str], encoding: str = ..., strings_only: bool = ..., errors: str = ...) -> bytes: ...
def force_bytes(s: Any, encoding: str = ..., strings_only: bool = ..., errors: str = ...) -> Union[bytes, date]: ...
def force_text(s: Any, encoding: str = ..., strings_only: bool = ..., errors: str = ...) -> Optional[str]: ...
def smart_bytes(s: Any, encoding: str = ..., strings_only: bool = ..., errors: str = ...) -> bytes: ...
def force_bytes(s: Any, encoding: str = ..., strings_only: bool = ..., errors: str = ...) -> bytes: ...

smart_str = smart_text
force_str = force_text
Expand Down
1 change: 1 addition & 0 deletions django-stubs/utils/functional.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union
from functools import wraps as wraps

from django.db.models.base import Model

Expand Down
2 changes: 1 addition & 1 deletion django-stubs/utils/html.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from typing import Any, Dict, Iterator, List, Optional, Tuple, Union

from django.db.models.base import Model
from django.db.models.fields.files import FieldFile
from django.utils.safestring import SafeText
from django.utils.safestring import SafeText, mark_safe as mark_safe

TRAILING_PUNCTUATION_CHARS: str
WRAPPING_PUNCTUATION: Any
Expand Down
4 changes: 2 additions & 2 deletions django-stubs/utils/termcolors.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import Any, Callable, Dict, Optional, Tuple, Union
from typing import Any, Callable, Dict, Optional, Tuple, Union, Sequence

color_names: Any
foreground: Any
background: Any
RESET: str
opt_dict: Any

def colorize(text: Optional[str] = ..., opts: Union[Tuple, str] = ..., **kwargs: Any) -> str: ...
def colorize(text: Optional[str] = ..., opts: Sequence[str] = ..., **kwargs: Any) -> str: ...
def make_style(opts: Tuple = ..., **kwargs: Any) -> Callable: ...

NOCOLOR_PALETTE: str
Expand Down
10 changes: 6 additions & 4 deletions mypy_django_plugin/plugins/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,12 @@ def get_related_field_type(rvalue: CallExpr, api: SemanticAnalyzerPass2,

def is_related_field(expr: CallExpr, module_file: MypyFile) -> bool:
if isinstance(expr.callee, MemberExpr) and isinstance(expr.callee.expr, NameExpr):
module = module_file.names[expr.callee.expr.name]
if module.fullname == 'django.db.models' and expr.callee.name in {'ForeignKey',
'OneToOneField',
'ManyToManyField'}:
module = module_file.names.get(expr.callee.expr.name)
if module \
and module.fullname == 'django.db.models' \
and expr.callee.name in {'ForeignKey',
'OneToOneField',
'ManyToManyField'}:
return True
return False

Expand Down

0 comments on commit d43cb1f

Please sign in to comment.