Skip to content

Commit b16340c

Browse files
committed
Improve stubs minimizing use of Any
1 parent 52456e0 commit b16340c

6 files changed

Lines changed: 43 additions & 36 deletions

File tree

stubs/geojson/geojson/base.pyi

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
from collections.abc import Callable, Iterable
1+
from _typeshed import Incomplete
2+
from collections.abc import Sequence
23
from typing import Any
34

45
class GeoJSON(dict[str, Any]):
5-
def __init__(self, iterable: Iterable[Any] = (), **extra) -> None: ...
6-
def __getattr__(self, name) -> Any: ...
7-
def __setattr__(self, name, value) -> None: ...
8-
def __delattr__(self, name) -> None: ...
6+
def __init__(self, iterable: Sequence[Incomplete] = (), **extra) -> None: ...
7+
def __getattr__(self, name: str | int) -> Incomplete: ...
8+
def __setattr__(self, name: str, value) -> None: ...
9+
def __delattr__(self, name: str) -> None: ...
910
@property
1011
def __geo_interface__(self) -> None | GeoJSON: ...
1112
@classmethod
12-
def to_instance(cls, ob: Any, default: Any = None, strict: bool = False) -> Any: ...
13+
def to_instance(cls, ob, default=None, strict: bool = False) -> GeoJSON: ...
1314
@property
1415
def is_valid(self) -> bool: ...
15-
def check_list_errors(self, checkFunc: Callable[..., Any], lst: list[Any]) -> list[str]: ...
16+
def check_list_errors(self, checkFunc, lst) -> list[str] | None: ...
1617
def errors(self) -> list[str] | None: ...

stubs/geojson/geojson/codec.pyi

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
import json
22
from _typeshed import SupportsRead, SupportsWrite
33
from collections.abc import Callable
4-
from typing import Any
4+
from typing import Any, NoReturn
55

6-
from geojson import GeoJSON
6+
from geojson.base import GeoJSON
77

88
class GeoJSONEncoder(json.JSONEncoder):
99
def default(self, obj) -> GeoJSON: ...
1010

1111
def dump(
12-
obj: Any, fp: SupportsWrite[str], cls: type[json.JSONEncoder] | None = json.JSONEncoder, allow_nan: bool = False, **kwargs
12+
obj, fp: SupportsWrite[str], cls: type[json.JSONEncoder] | None = json.JSONEncoder, allow_nan: bool = False, **kwargs
1313
) -> None: ...
1414
def dumps(
15-
obj: Any, cls: type[json.JSONEncoder] | None = json.JSONEncoder, allow_nan: bool = False, ensure_ascii: bool = False, **kwargs
15+
obj, cls: type[json.JSONEncoder] | None = json.JSONEncoder, allow_nan: bool = False, ensure_ascii: bool = False, **kwargs
1616
) -> str: ...
1717
def load(
1818
fp: SupportsRead[str],
1919
cls: type[json.JSONDecoder] = json.JSONDecoder,
20-
parse_constant: Callable[[Any], None] = ...,
21-
object_hook: Callable[..., GeoJSON] = GeoJSON.to_instance,
20+
parse_constant: Callable[..., NoReturn] = ...,
21+
object_hook: Callable[[dict[str, Any]], GeoJSON] = GeoJSON.to_instance,
2222
**kwargs,
2323
) -> GeoJSON: ...
2424
def loads(
2525
s: str,
2626
cls: type[json.JSONDecoder] = json.JSONDecoder,
27-
parse_constant: Callable[[Any], None] = ...,
28-
object_hook: Callable[..., GeoJSON] = GeoJSON.to_instance,
27+
parse_constant: Callable[..., NoReturn] = ...,
28+
object_hook: Callable[[dict[str, Any]], GeoJSON] = GeoJSON.to_instance,
2929
**kwargs,
3030
) -> GeoJSON: ...
3131

stubs/geojson/geojson/examples.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any, Literal
22

3-
from geojson import GeoJSON
3+
from geojson.base import GeoJSON
44
from geojson.geometry import Geometry
55

66
class SimpleWebFeature:

stubs/geojson/geojson/feature.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ class Feature(GeoJSON):
1212
class FeatureCollection(GeoJSON):
1313
def __init__(self, features: list[Feature | Geometry], **extra) -> None: ...
1414
def errors(self) -> list[str] | None: ...
15-
def __getitem__(self, key) -> Feature: ...
15+
def __getitem__(self, key: int | str) -> Feature: ...

stubs/geojson/geojson/geometry.pyi

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
from collections.abc import Iterable
2-
from typing import Any
1+
from collections.abc import Sequence
2+
from decimal import Decimal
3+
from typing import Literal, TypeAlias
34

4-
from geojson import Feature
55
from geojson.base import GeoJSON
66

7-
DEFAULT_PRECISION: int
7+
_InputCoord: TypeAlias = float | Decimal | Geometry | Sequence[_InputCoord]
8+
_CleanCoord: TypeAlias = float | Decimal | list[_CleanCoord]
9+
10+
DEFAULT_PRECISION: Literal[6]
811

912
class Geometry(GeoJSON):
1013
def __init__(
11-
self, coordinates: None | Feature | Iterable[Any] = None, validate: bool = False, precision: None | int = None, **extra
14+
self,
15+
coordinates: None | Sequence[_InputCoord] | Geometry = None,
16+
validate: bool = False,
17+
precision: None | int = None,
18+
**extra,
1219
) -> None: ...
1320
@classmethod
14-
def clean_coordinates(
15-
cls, coords: Geometry | tuple[Any, ...] | list[tuple[Any, ...]], precision: None | int
16-
) -> None | tuple[Any, ...] | list[tuple[Any, ...]]: ...
21+
def clean_coordinates(cls, coords: Sequence[_InputCoord] | Geometry, precision: int) -> list[_CleanCoord]: ...
1722

1823
class GeometryCollection(GeoJSON):
19-
def __init__(self, geometries=..., **extra) -> None: ...
24+
def __init__(self, geometries: None | Sequence[Geometry] = None, **extra) -> None: ...
2025
def errors(self) -> list[str] | None: ...
2126
def __getitem__(self, key) -> Geometry | tuple[()] | None: ...
2227

@@ -30,10 +35,10 @@ class MultiPoint(Geometry):
3035

3136
def check_line_string(coord) -> str | None: ...
3237

33-
class LineString(MultiPoint):
38+
class LineString(Geometry):
3439
def errors(self) -> list[str] | None: ...
3540

36-
class MultiLineString(Geometry):
41+
class MultiLineString(MultiPoint):
3742
def errors(self) -> list[str] | None: ...
3843

3944
def check_polygon(coord) -> str | None: ...

stubs/geojson/geojson/utils.pyi

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
from collections.abc import Generator
1+
from _typeshed import Incomplete
2+
from collections.abc import Callable, Generator
23
from typing import Any, Literal
34

4-
from geojson import Feature, GeoJSON
5-
from geojson.geometry import Geometry
5+
from geojson.base import GeoJSON
6+
from geojson.geometry import Geometry, LineString, Point, Polygon
67

7-
def coords(obj: Feature | Geometry | dict[str, Any]) -> Generator[tuple[Any]]: ...
8-
def map_coords(func, obj) -> dict[str, Any]: ...
9-
def map_tuples(func, obj) -> list[tuple[float]]: ...
10-
def map_geometries(func, obj) -> GeoJSON: ...
8+
def coords(obj: GeoJSON | dict[str, Any]) -> Generator[tuple[float]]: ...
9+
def map_coords(func: Callable[[Incomplete], float | Geometry], obj: GeoJSON | dict[str, Any]) -> dict[str, Any]: ...
10+
def map_tuples(func: Callable[[Incomplete], float | Geometry], obj: GeoJSON | dict[str, Any]) -> dict[str, Any]: ...
11+
def map_geometries(func: Callable[[Incomplete], float | Geometry], obj: GeoJSON | dict[str, Any]) -> dict[str, Any]: ...
1112
def generate_random(
1213
featureType: Literal["Point", "LineString", "Polygon"],
1314
numberVertices: int = 3,
1415
boundingBox: list[float] = [-180.0, -90.0, 180.0, 90.0],
15-
) -> Feature: ...
16+
) -> Point | LineString | Polygon: ...

0 commit comments

Comments
 (0)