-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
safe generic thanks to python/typing#498
- Loading branch information
1 parent
a52ed9d
commit 813ee95
Showing
3 changed files
with
41 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
""" | ||
This module exports a safe generic type and a type variable T to get around the bug in Python 3.5 | ||
https://github.com/python/typing/issues/498 | ||
""" | ||
import typing as tp | ||
import sys | ||
|
||
__all__ = ['T', 'SafeGeneric'] | ||
|
||
T = tp.TypeVar('T') | ||
|
||
if sys.version_info[1] == 5: | ||
SafeGeneric = object | ||
else: | ||
SafeGeneric = tp.Generic[T] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
813ee95
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably only an incompatible hack that doesn't work as a correct Generic type e.g with mypy 0.770 and Python 3.7
The error message in mypy is
"MyClass" expects no type arguments, but 1 given
if I use MyClass[T]or 'Variable
SafeGeneric" is not valid as a type
if the variable SafeGeneric is moved to the same moduleor
Incompatible types in assignment (expression has type "MyClass[<nothing>]", variable has type "MyClass[T]")
A solution that works:
This is safe:
__copy__ = None
has the same effect for the function copy.copy() like an undefined__copy__
attribute. A possible problem is if it would overshadow the right__copy__
attribute.__copy__
method, but it can be easily solved by asuper(...).__copy__()
)