mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-07-11 12:28:03 +00:00
Document more uses of type ignore
This commit is contained in:
parent
51b79411e3
commit
d01e73fca5
@ -802,7 +802,9 @@ class GuildChannel:
|
|||||||
await http.delete_channel_permissions(self.id, target.id, reason=reason)
|
await http.delete_channel_permissions(self.id, target.id, reason=reason)
|
||||||
elif isinstance(overwrite, PermissionOverwrite):
|
elif isinstance(overwrite, PermissionOverwrite):
|
||||||
(allow, deny) = overwrite.pair()
|
(allow, deny) = overwrite.pair()
|
||||||
await http.edit_channel_permissions(self.id, target.id, str(allow.value), str(deny.value), perm_type, reason=reason)
|
await http.edit_channel_permissions(
|
||||||
|
self.id, target.id, str(allow.value), str(deny.value), perm_type, reason=reason
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
raise InvalidArgument('Invalid overwrite type provided.')
|
raise InvalidArgument('Invalid overwrite type provided.')
|
||||||
|
|
||||||
|
@ -126,7 +126,7 @@ class ActionRow(Component):
|
|||||||
return {
|
return {
|
||||||
'type': int(self.type),
|
'type': int(self.type),
|
||||||
'components': [child.to_dict() for child in self.children],
|
'components': [child.to_dict() for child in self.children],
|
||||||
} # type: ignore
|
} # type: ignore - Type checker does not understand these are the same
|
||||||
|
|
||||||
|
|
||||||
class Button(Component):
|
class Button(Component):
|
||||||
@ -198,7 +198,7 @@ class Button(Component):
|
|||||||
if self.emoji:
|
if self.emoji:
|
||||||
payload['emoji'] = self.emoji.to_dict()
|
payload['emoji'] = self.emoji.to_dict()
|
||||||
|
|
||||||
return payload # type: ignore
|
return payload # type: ignore - Type checker does not understand these are the same
|
||||||
|
|
||||||
|
|
||||||
class SelectMenu(Component):
|
class SelectMenu(Component):
|
||||||
@ -364,7 +364,7 @@ class SelectOption:
|
|||||||
}
|
}
|
||||||
|
|
||||||
if self.emoji:
|
if self.emoji:
|
||||||
payload['emoji'] = self.emoji.to_dict() # type: ignore
|
payload['emoji'] = self.emoji.to_dict() # type: ignore - This Dict[str, Any] is compatible with PartialEmoji
|
||||||
|
|
||||||
if self.description:
|
if self.description:
|
||||||
payload['description'] = self.description
|
payload['description'] = self.description
|
||||||
@ -462,10 +462,13 @@ def _component_factory(data: ComponentPayload) -> Component:
|
|||||||
if component_type == 1:
|
if component_type == 1:
|
||||||
return ActionRow(data)
|
return ActionRow(data)
|
||||||
elif component_type == 2:
|
elif component_type == 2:
|
||||||
|
# The type checker does not properly do narrowing here.
|
||||||
return Button(data) # type: ignore
|
return Button(data) # type: ignore
|
||||||
elif component_type == 3:
|
elif component_type == 3:
|
||||||
|
# The type checker does not properly do narrowing here.
|
||||||
return SelectMenu(data) # type: ignore
|
return SelectMenu(data) # type: ignore
|
||||||
elif component_type == 4:
|
elif component_type == 4:
|
||||||
|
# The type checker does not properly do narrowing here.
|
||||||
return TextInput(data) # type: ignore
|
return TextInput(data) # type: ignore
|
||||||
else:
|
else:
|
||||||
as_enum = try_enum(ComponentType, component_type)
|
as_enum = try_enum(ComponentType, component_type)
|
||||||
|
@ -311,7 +311,7 @@ class Embed:
|
|||||||
return getattr(self, '_colour', EmptyEmbed)
|
return getattr(self, '_colour', EmptyEmbed)
|
||||||
|
|
||||||
@colour.setter
|
@colour.setter
|
||||||
def colour(self, value: Union[int, Colour, _EmptyEmbed]): # type: ignore
|
def colour(self, value: Union[int, Colour, _EmptyEmbed]):
|
||||||
if isinstance(value, (Colour, _EmptyEmbed)):
|
if isinstance(value, (Colour, _EmptyEmbed)):
|
||||||
self._colour = value
|
self._colour = value
|
||||||
elif isinstance(value, int):
|
elif isinstance(value, int):
|
||||||
@ -344,6 +344,7 @@ class Embed:
|
|||||||
|
|
||||||
If the attribute has no value then :attr:`Empty` is returned.
|
If the attribute has no value then :attr:`Empty` is returned.
|
||||||
"""
|
"""
|
||||||
|
# Lying to the type checker for better developer UX.
|
||||||
return EmbedProxy(getattr(self, '_footer', {})) # type: ignore
|
return EmbedProxy(getattr(self, '_footer', {})) # type: ignore
|
||||||
|
|
||||||
def set_footer(self: E, *, text: MaybeEmpty[Any] = EmptyEmbed, icon_url: MaybeEmpty[Any] = EmptyEmbed) -> E:
|
def set_footer(self: E, *, text: MaybeEmpty[Any] = EmptyEmbed, icon_url: MaybeEmpty[Any] = EmptyEmbed) -> E:
|
||||||
@ -397,6 +398,7 @@ class Embed:
|
|||||||
|
|
||||||
If the attribute has no value then :attr:`Empty` is returned.
|
If the attribute has no value then :attr:`Empty` is returned.
|
||||||
"""
|
"""
|
||||||
|
# Lying to the type checker for better developer UX.
|
||||||
return EmbedProxy(getattr(self, '_image', {})) # type: ignore
|
return EmbedProxy(getattr(self, '_image', {})) # type: ignore
|
||||||
|
|
||||||
def set_image(self: E, *, url: MaybeEmpty[Any]) -> E:
|
def set_image(self: E, *, url: MaybeEmpty[Any]) -> E:
|
||||||
@ -439,6 +441,7 @@ class Embed:
|
|||||||
|
|
||||||
If the attribute has no value then :attr:`Empty` is returned.
|
If the attribute has no value then :attr:`Empty` is returned.
|
||||||
"""
|
"""
|
||||||
|
# Lying to the type checker for better developer UX.
|
||||||
return EmbedProxy(getattr(self, '_thumbnail', {})) # type: ignore
|
return EmbedProxy(getattr(self, '_thumbnail', {})) # type: ignore
|
||||||
|
|
||||||
def set_thumbnail(self: E, *, url: MaybeEmpty[Any]) -> E:
|
def set_thumbnail(self: E, *, url: MaybeEmpty[Any]) -> E:
|
||||||
@ -480,6 +483,7 @@ class Embed:
|
|||||||
|
|
||||||
If the attribute has no value then :attr:`Empty` is returned.
|
If the attribute has no value then :attr:`Empty` is returned.
|
||||||
"""
|
"""
|
||||||
|
# Lying to the type checker for better developer UX.
|
||||||
return EmbedProxy(getattr(self, '_video', {})) # type: ignore
|
return EmbedProxy(getattr(self, '_video', {})) # type: ignore
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -490,6 +494,7 @@ class Embed:
|
|||||||
|
|
||||||
If the attribute has no value then :attr:`Empty` is returned.
|
If the attribute has no value then :attr:`Empty` is returned.
|
||||||
"""
|
"""
|
||||||
|
# Lying to the type checker for better developer UX.
|
||||||
return EmbedProxy(getattr(self, '_provider', {})) # type: ignore
|
return EmbedProxy(getattr(self, '_provider', {})) # type: ignore
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -500,6 +505,7 @@ class Embed:
|
|||||||
|
|
||||||
If the attribute has no value then :attr:`Empty` is returned.
|
If the attribute has no value then :attr:`Empty` is returned.
|
||||||
"""
|
"""
|
||||||
|
# Lying to the type checker for better developer UX.
|
||||||
return EmbedProxy(getattr(self, '_author', {})) # type: ignore
|
return EmbedProxy(getattr(self, '_author', {})) # type: ignore
|
||||||
|
|
||||||
def set_author(self: E, *, name: Any, url: MaybeEmpty[Any] = EmptyEmbed, icon_url: MaybeEmpty[Any] = EmptyEmbed) -> E:
|
def set_author(self: E, *, name: Any, url: MaybeEmpty[Any] = EmptyEmbed, icon_url: MaybeEmpty[Any] = EmptyEmbed) -> E:
|
||||||
@ -553,6 +559,7 @@ class Embed:
|
|||||||
|
|
||||||
If the attribute has no value then :attr:`Empty` is returned.
|
If the attribute has no value then :attr:`Empty` is returned.
|
||||||
"""
|
"""
|
||||||
|
# Lying to the type checker for better developer UX.
|
||||||
return [EmbedProxy(d) for d in getattr(self, '_fields', [])] # type: ignore
|
return [EmbedProxy(d) for d in getattr(self, '_fields', [])] # type: ignore
|
||||||
|
|
||||||
def add_field(self: E, *, name: Any, value: Any, inline: bool = True) -> E:
|
def add_field(self: E, *, name: Any, value: Any, inline: bool = True) -> E:
|
||||||
@ -726,4 +733,4 @@ class Embed:
|
|||||||
if self.title:
|
if self.title:
|
||||||
result['title'] = self.title
|
result['title'] = self.title
|
||||||
|
|
||||||
return result # type: ignore
|
return result # type: ignore - This payload is equivalent to the EmbedData type
|
||||||
|
@ -24,7 +24,7 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
|
|
||||||
import types
|
import types
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from typing import Any, ClassVar, Dict, List, Optional, TYPE_CHECKING, Type, TypeVar
|
from typing import Any, ClassVar, Dict, List, Optional, TYPE_CHECKING, Tuple, Type, TypeVar
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'Enum',
|
'Enum',
|
||||||
@ -59,15 +59,17 @@ __all__ = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _create_value_cls(name, comparable):
|
def _create_value_cls(name: str, comparable: bool):
|
||||||
|
# All the type ignores here are due to the type checker being unable to recognise
|
||||||
|
# Runtime type creation without exploding.
|
||||||
cls = namedtuple('_EnumValue_' + name, 'name value')
|
cls = namedtuple('_EnumValue_' + name, 'name value')
|
||||||
cls.__repr__ = lambda self: f'<{name}.{self.name}: {self.value!r}>'
|
cls.__repr__ = lambda self: f'<{name}.{self.name}: {self.value!r}>' # type: ignore
|
||||||
cls.__str__ = lambda self: f'{name}.{self.name}'
|
cls.__str__ = lambda self: f'{name}.{self.name}' # type: ignore
|
||||||
if comparable:
|
if comparable:
|
||||||
cls.__le__ = lambda self, other: isinstance(other, self.__class__) and self.value <= other.value
|
cls.__le__ = lambda self, other: isinstance(other, self.__class__) and self.value <= other.value # type: ignore
|
||||||
cls.__ge__ = lambda self, other: isinstance(other, self.__class__) and self.value >= other.value
|
cls.__ge__ = lambda self, other: isinstance(other, self.__class__) and self.value >= other.value # type: ignore
|
||||||
cls.__lt__ = lambda self, other: isinstance(other, self.__class__) and self.value < other.value
|
cls.__lt__ = lambda self, other: isinstance(other, self.__class__) and self.value < other.value # type: ignore
|
||||||
cls.__gt__ = lambda self, other: isinstance(other, self.__class__) and self.value > other.value
|
cls.__gt__ = lambda self, other: isinstance(other, self.__class__) and self.value > other.value # type: ignore
|
||||||
return cls
|
return cls
|
||||||
|
|
||||||
|
|
||||||
@ -82,7 +84,7 @@ class EnumMeta(type):
|
|||||||
_enum_member_map_: ClassVar[Dict[str, Any]]
|
_enum_member_map_: ClassVar[Dict[str, Any]]
|
||||||
_enum_value_map_: ClassVar[Dict[Any, Any]]
|
_enum_value_map_: ClassVar[Dict[Any, Any]]
|
||||||
|
|
||||||
def __new__(cls, name, bases, attrs, *, comparable: bool = False):
|
def __new__(cls: Type[type], name: str, bases: Tuple[type, ...], attrs: Dict[str, Any], *, comparable: bool = False):
|
||||||
value_mapping = {}
|
value_mapping = {}
|
||||||
member_mapping = {}
|
member_mapping = {}
|
||||||
member_names = []
|
member_names = []
|
||||||
@ -117,7 +119,7 @@ class EnumMeta(type):
|
|||||||
attrs['_enum_member_names_'] = member_names
|
attrs['_enum_member_names_'] = member_names
|
||||||
attrs['_enum_value_cls_'] = value_cls
|
attrs['_enum_value_cls_'] = value_cls
|
||||||
actual_cls = super().__new__(cls, name, bases, attrs)
|
actual_cls = super().__new__(cls, name, bases, attrs)
|
||||||
value_cls._actual_enum_cls_ = actual_cls # type: ignore
|
value_cls._actual_enum_cls_ = actual_cls # type: ignore - Runtime attribute isn't understood
|
||||||
return actual_cls
|
return actual_cls
|
||||||
|
|
||||||
def __iter__(cls):
|
def __iter__(cls):
|
||||||
@ -607,22 +609,22 @@ class NSFWLevel(Enum, comparable=True):
|
|||||||
age_restricted = 3
|
age_restricted = 3
|
||||||
|
|
||||||
|
|
||||||
T = TypeVar('T')
|
E = TypeVar('E', bound='Enum')
|
||||||
|
|
||||||
|
|
||||||
def create_unknown_value(cls: Type[T], val: Any) -> T:
|
def create_unknown_value(cls: Type[E], val: Any) -> E:
|
||||||
value_cls = cls._enum_value_cls_ # type: ignore
|
value_cls = cls._enum_value_cls_ # type: ignore - This is narrowed below
|
||||||
name = f'unknown_{val}'
|
name = f'unknown_{val}'
|
||||||
return value_cls(name=name, value=val)
|
return value_cls(name=name, value=val)
|
||||||
|
|
||||||
|
|
||||||
def try_enum(cls: Type[T], val: Any) -> T:
|
def try_enum(cls: Type[E], val: Any) -> E:
|
||||||
"""A function that tries to turn the value into enum ``cls``.
|
"""A function that tries to turn the value into enum ``cls``.
|
||||||
|
|
||||||
If it fails it returns a proxy invalid value instead.
|
If it fails it returns a proxy invalid value instead.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return cls._enum_value_map_[val] # type: ignore
|
return cls._enum_value_map_[val] # type: ignore - All errors are caught below
|
||||||
except (KeyError, TypeError, AttributeError):
|
except (KeyError, TypeError, AttributeError):
|
||||||
return create_unknown_value(cls, val)
|
return create_unknown_value(cls, val)
|
||||||
|
@ -125,7 +125,7 @@ class HTTPException(DiscordException):
|
|||||||
|
|
||||||
def __init__(self, response: _ResponseType, message: Optional[Union[str, Dict[str, Any]]]):
|
def __init__(self, response: _ResponseType, message: Optional[Union[str, Dict[str, Any]]]):
|
||||||
self.response: _ResponseType = response
|
self.response: _ResponseType = response
|
||||||
self.status: int = response.status # type: ignore
|
self.status: int = response.status # type: ignore - This attribute is filled by the library even if using requests
|
||||||
self.code: int
|
self.code: int
|
||||||
self.text: str
|
self.text: str
|
||||||
if isinstance(message, dict):
|
if isinstance(message, dict):
|
||||||
|
@ -47,6 +47,7 @@ __all__ = (
|
|||||||
V = TypeVar('V', bound='View', covariant=True)
|
V = TypeVar('V', bound='View', covariant=True)
|
||||||
TI = TypeVar('TI', bound='TextInput')
|
TI = TypeVar('TI', bound='TextInput')
|
||||||
|
|
||||||
|
|
||||||
class TextInput(Item[V]):
|
class TextInput(Item[V]):
|
||||||
"""Represents a UI text input.
|
"""Represents a UI text input.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user