Fix type annotations for django.utils.safestring (#179)

* Add/fix types for django.utils.safestring.mark_safe

Django code ref: 964dd4f4f2/django/utils/safestring.py (L71-L84)

* add generic annotations for mark_safe, remove SafeBytes as it is basically deprecated

Co-authored-by: Daniel Hahler <github@thequod.de>
This commit is contained in:
Maxim Kurnikov
2019-09-23 21:16:24 +03:00
parent afcd0d9293
commit 7407b93151
3 changed files with 45 additions and 15 deletions

View File

@@ -39,4 +39,24 @@
# Ensure that the method's type is preserved
reveal_type(ClassWithAtomicMethod().atomic_method1) # N: Revealed type is 'def (abc: builtins.int) -> builtins.str'
# Ensure that the method's type is preserved
reveal_type(ClassWithAtomicMethod().atomic_method3) # N: Revealed type is 'def (myparam: builtins.str) -> builtins.int'
reveal_type(ClassWithAtomicMethod().atomic_method3) # N: Revealed type is 'def (myparam: builtins.str) -> builtins.int'
- case: mark_safe_decorator_and_function
main: |
from django.utils.safestring import mark_safe
s = 'hello'
reveal_type(mark_safe(s)) # N: Revealed type is 'django.utils.safestring.SafeText'
reveal_type(mark_safe(s) + mark_safe(s)) # N: Revealed type is 'django.utils.safestring.SafeText'
reveal_type(s + mark_safe(s)) # N: Revealed type is 'builtins.str'
s += mark_safe(s)
reveal_type(s) # N: Revealed type is 'builtins.str'
ms = mark_safe(s)
ms += mark_safe(s)
reveal_type(ms) # N: Revealed type is 'django.utils.safestring.SafeText'
@mark_safe
def func(s: str) -> str:
pass
reveal_type(func) # N: Revealed type is 'def (s: builtins.str) -> builtins.str'