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

Manual changes of Any union to Incomplete in stubs folder #9566

Merged
merged 9 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 5 additions & 4 deletions stubs/PyYAML/yaml/nodes.pyi
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
from _typeshed import Incomplete
from typing import Any, ClassVar

from yaml.error import Mark

class Node:
tag: str
value: Any
start_mark: Mark | Any
end_mark: Mark | Any
start_mark: Mark | Incomplete
Copy link
Member

Choose a reason for hiding this comment

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

This kind of looks like it's using the | Any trick for when the type is almost always one particular type (like we do for re.match). If so, Any is right.

Copy link
Sponsor Collaborator Author

Choose a reason for hiding this comment

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

Those attributes are directly assigned from the parameters, so they should be the same.
If it's possible, and intended to be anything else than Mark | None, with an arbitrary type, or if it's to avoid having to check for a None mark when you know you instantiated it not-None. And that's a common use-case/pattern. That can be represented using generics. Since it's feasible within the current typing system, Incomplete (could be done but we didn't bothered) seems preferable over Any (can't be done, we tried)

Copy link
Sponsor Collaborator Author

@Avasam Avasam Feb 1, 2023

Choose a reason for hiding this comment

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

I think there's an argument to be made here for either permissive unions or default generics. Which both aren't part of the type system atm. I've changed the type to Mark | None | AnyMark | Any and added a comment to explain.

end_mark: Mark | Incomplete
def __init__(self, tag: str, value, start_mark: Mark | None, end_mark: Mark | None) -> None: ...

class ScalarNode(Node):
id: ClassVar[str]
style: str | Any
style: str | Incomplete
def __init__(
self, tag: str, value, start_mark: Mark | None = ..., end_mark: Mark | None = ..., style: str | None = ...
) -> None: ...

class CollectionNode(Node):
flow_style: bool | Any
flow_style: bool | Incomplete
def __init__(
self, tag: str, value, start_mark: Mark | None = ..., end_mark: Mark | None = ..., flow_style: bool | None = ...
) -> None: ...
Expand Down
6 changes: 3 additions & 3 deletions stubs/PyYAML/yaml/representer.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import datetime
from _typeshed import SupportsItems
from _typeshed import Incomplete, SupportsItems
from collections.abc import Callable, Iterable, Mapping
from types import BuiltinFunctionType, FunctionType, ModuleType
from typing import Any, ClassVar, NoReturn, TypeVar
Expand All @@ -15,12 +15,12 @@ class RepresenterError(YAMLError): ...
class BaseRepresenter:
yaml_representers: ClassVar[dict[type[Any], Callable[[BaseRepresenter, Any], Node]]]
yaml_multi_representers: ClassVar[dict[type[Any], Callable[[BaseRepresenter, Any], Node]]]
default_style: str | Any
default_style: str | Incomplete
sort_keys: bool
default_flow_style: bool
represented_objects: dict[int, Node]
object_keeper: list[Any]
alias_key: int | Any
alias_key: int | Incomplete
def __init__(self, default_style: str | None = ..., default_flow_style: bool = ..., sort_keys: bool = ...) -> None: ...
def represent(self, data) -> None: ...
def represent_data(self, data) -> Node: ...
Expand Down
7 changes: 5 additions & 2 deletions stubs/SQLAlchemy/sqlalchemy/dialects/postgresql/array.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from _typeshed import Incomplete
from typing import Any as _Any

import sqlalchemy.types as sqltypes
Expand All @@ -13,7 +14,7 @@ class array(expression.ClauseList, expression.ColumnElement[_Any]):
inherit_cache: bool
type: _Any
def __init__(self, clauses, **kw) -> None: ...
def self_group(self, against: _Any | None = ...): ...
def self_group(self, against: Incomplete | None = ...): ...

CONTAINS: _Any
CONTAINED_BY: _Any
Expand All @@ -29,7 +30,9 @@ class ARRAY(sqltypes.ARRAY):
as_tuple: _Any
dimensions: _Any
zero_indexes: _Any
def __init__(self, item_type, as_tuple: bool = ..., dimensions: _Any | None = ..., zero_indexes: bool = ...) -> None: ...
def __init__(
self, item_type, as_tuple: bool = ..., dimensions: Incomplete | None = ..., zero_indexes: bool = ...
) -> None: ...
@property
def hashable(self): ...
@property
Expand Down
5 changes: 3 additions & 2 deletions stubs/paho-mqtt/paho/mqtt/client.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import socket as _socket
import ssl as _ssl
import time
import types
from _typeshed import Incomplete
from collections.abc import Callable
from typing import Any, Optional, TypeVar
from typing_extensions import TypeAlias
Expand All @@ -16,7 +17,7 @@ ssl: types.ModuleType | None
socks: types.ModuleType | None
time_func = time.monotonic
HAVE_DNS: bool
EAGAIN: int | Any
EAGAIN: int | Incomplete
MQTTv31: int
MQTTv311: int
MQTTv5: int
Expand Down Expand Up @@ -86,7 +87,7 @@ MQTT_BRIDGE: int
MQTT_CLEAN_START_FIRST_ONLY: int
sockpair_data: bytes
_UserData: TypeAlias = Any
_Socket: TypeAlias = _socket.socket | _ssl.SSLSocket | Any
_Socket: TypeAlias = _socket.socket | _ssl.SSLSocket | Incomplete
_Payload: TypeAlias = str | bytes | bytearray | float
_ExtraHeader: TypeAlias = dict[str, str] | Callable[[dict[str, str]], dict[str, str]]
_OnLog: TypeAlias = Callable[[Client, _UserData, int, str], object]
Expand Down
3 changes: 2 additions & 1 deletion stubs/parsimonious/parsimonious/grammar.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import collections.abc
from _typeshed import Incomplete
from collections import OrderedDict
from collections.abc import Callable, Mapping
from typing import Any, NoReturn
Expand All @@ -7,7 +8,7 @@ from parsimonious.expressions import _CALLABLE_TYPE, Expression, Literal, Lookah
from parsimonious.nodes import Node, NodeVisitor

class Grammar(OrderedDict[str, Expression]):
default_rule: Expression | Any
default_rule: Expression | Incomplete
def __init__(self, rules: str = ..., **more_rules: Expression | _CALLABLE_TYPE) -> None: ...
def default(self, rule_name: str) -> Grammar: ...
def parse(self, text: str, pos: int = ...) -> Node: ...
Expand Down
3 changes: 2 additions & 1 deletion stubs/parsimonious/parsimonious/nodes.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from _typeshed import Incomplete
from collections.abc import Callable, Iterator, Sequence
from re import Match
from typing import Any, NoReturn, TypeVar
Expand Down Expand Up @@ -27,7 +28,7 @@ class RegexNode(Node):
class RuleDecoratorMeta(type): ...

class NodeVisitor(metaclass=RuleDecoratorMeta):
grammar: Grammar | Any
grammar: Grammar | Incomplete
unwrapped_exceptions: tuple[type[BaseException], ...]
def visit(self, node: Node) -> Any: ...
def generic_visit(self, node: Node, visited_children: Sequence[Any]) -> NoReturn: ...
Expand Down
2 changes: 1 addition & 1 deletion stubs/passlib/passlib/utils/handlers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class HasRounds(GenericHandler):
min_desired_rounds: ClassVar[int | None]
max_desired_rounds: ClassVar[int | None]
default_rounds: ClassVar[int | None]
vary_rounds: ClassVar[Any | None]
vary_rounds: ClassVar[Incomplete | None]
rounds: int
@classmethod
def using( # type: ignore[override]
Expand Down
6 changes: 3 additions & 3 deletions stubs/protobuf/google/protobuf/internal/well_known_types.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from _typeshed import SupportsItems
from _typeshed import Incomplete, SupportsItems
from collections.abc import Iterable, Iterator, KeysView, Mapping, Sequence
from datetime import datetime, timedelta, tzinfo
from typing import Any as tAny
Expand All @@ -9,7 +9,7 @@ from google.protobuf import struct_pb2
class Any:
type_url: tAny = ...
value: tAny = ...
def Pack(self, msg: tAny, type_url_prefix: str = ..., deterministic: tAny | None = ...) -> None: ...
def Pack(self, msg: tAny, type_url_prefix: str = ..., deterministic: Incomplete | None = ...) -> None: ...
def Unpack(self, msg: tAny) -> bool: ...
def TypeName(self) -> str: ...
def Is(self, descriptor: tAny) -> bool: ...
Expand Down Expand Up @@ -60,7 +60,7 @@ class FieldMask:
) -> None: ...

class _FieldMaskTree:
def __init__(self, field_mask: tAny | None = ...) -> None: ...
def __init__(self, field_mask: Incomplete | None = ...) -> None: ...
def MergeFromFieldMask(self, field_mask: tAny) -> None: ...
def AddPath(self, path: tAny): ...
def ToFieldMask(self, field_mask: tAny) -> None: ...
Expand Down
8 changes: 4 additions & 4 deletions stubs/redis/redis/lock.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from _typeshed import Self
from _typeshed import Incomplete, Self
from types import TracebackType
from typing import Any, ClassVar, Protocol

Expand All @@ -11,9 +11,9 @@ class Lock:
LUA_EXTEND_SCRIPT: ClassVar[str]
LUA_REACQUIRE_SCRIPT: ClassVar[str]
LUA_RELEASE_SCRIPT: ClassVar[str]
lua_extend: ClassVar[Any | None]
lua_reacquire: ClassVar[Any | None]
lua_release: ClassVar[Any | None]
lua_extend: ClassVar[Incomplete | None]
lua_reacquire: ClassVar[Incomplete | None]
lua_release: ClassVar[Incomplete | None]
local: _Local
def __init__(
self,
Expand Down
4 changes: 2 additions & 2 deletions stubs/toml/toml/encoder.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class TomlEncoder(Generic[_MappingT]):
def __init__(self: TomlEncoder[dict[str, Any]], _dict: type[dict[str, Any]] = ..., preserve: bool = ...) -> None: ...
def get_empty_table(self) -> _MappingT: ...
def dump_list(self, v: Iterable[object]) -> str: ...
def dump_inline_table(self, section: dict[str, Any] | Any) -> str: ...
def dump_value(self, v: Any) -> str: ...
def dump_inline_table(self, section: object) -> str: ...
def dump_value(self, v: object) -> str: ...
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure this should be object? I don't think toml can dump all objects.

Copy link
Sponsor Collaborator Author

@Avasam Avasam Feb 1, 2023

Choose a reason for hiding this comment

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

From what I can see of the implementation, and from trying it, if it doesn't specifically handle an object it just stringifies it.

>>> TomlEncoder().dump_inline_table(object()) 
'"<object object at 0x000001FF99FF85A0>"'
>>> TomlEncoder().dump_value(object())        
'"<object object at 0x000001FF99FF85A0>"'
>>> TomlEncoder().dump_value(dict())   
'[]'
>>> TomlEncoder().dump_value([object()]) 
'[ "<object object at 0x000001FF99FF85A0>",]'
>>> TomlEncoder().dump_inline_table([object()]) 
'[ "<object object at 0x000001FF99FF85B0>",]'
>>> TomlEncoder().dump_inline_table({"aaa": object()}) 
'{ aaa = "<object object at 0x000001FF99FF85A0>" }\n'
>>> TomlEncoder().dump_value({"aaa": object()})        
'[ "aaa",]'

Copy link
Collaborator

Choose a reason for hiding this comment

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

While technically it can handle all objects, in that it doesn't throw an exception, not all objects are correct arguments. "<object object at 0x000001FF99FF85B0>" is obviously not useful as an output.

Copy link
Sponsor Collaborator Author

@Avasam Avasam Feb 1, 2023

Choose a reason for hiding this comment

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

And trying to represent intended values, we kindof hit the same issue as JSON.
There's also classes with custom repr that can be dumped.
And subclassed Encoder that extend dump_funcs to support more intended types.

To be consistent though, how about the dump_list method typed with Iterable[object] ? I feel the same logic should apply and be Iterable[Any]. (it's calling dump_value internally)

Copy link
Member

Choose a reason for hiding this comment

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

That's why Any makes sense here: the type annotation we'd ideally want isn't representable in the type system.

def dump_sections(self, o: _MappingT, sup: str) -> tuple[str, _MappingT]: ...

class TomlPreserveInlineDictEncoder(TomlEncoder[_MappingT]):
Expand Down