Skip to content

Commit b4b4b91

Browse files
committed
import python type annotations only when type checking for import times
1 parent e599088 commit b4b4b91

8 files changed

Lines changed: 138 additions & 107 deletions

File tree

python/mesc/__init__.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,34 @@
99
find_endpoints,
1010
get_global_metadata,
1111
)
12-
from .types import Endpoint, Profile, RpcConfig
1312

14-
__version__ = '0.2.0'
13+
import typing
1514

16-
__all__ = (
17-
'Endpoint',
18-
'Profile',
19-
'RpcConfig',
20-
'is_mesc_enabled',
21-
'get_default_endpoint',
22-
'get_endpoint_by_name',
23-
'get_endpoint_by_network',
24-
'get_endpoint_by_query',
25-
'find_endpoints',
26-
'get_global_metadata',
27-
)
15+
if typing.TYPE_CHECKING:
16+
from .types import Endpoint, Profile, RpcConfig
17+
18+
__all__ = (
19+
'Endpoint',
20+
'Profile',
21+
'RpcConfig',
22+
'is_mesc_enabled',
23+
'get_default_endpoint',
24+
'get_endpoint_by_name',
25+
'get_endpoint_by_network',
26+
'get_endpoint_by_query',
27+
'find_endpoints',
28+
'get_global_metadata',
29+
)
30+
31+
else:
32+
__all__ = (
33+
'is_mesc_enabled',
34+
'get_default_endpoint',
35+
'get_endpoint_by_name',
36+
'get_endpoint_by_network',
37+
'get_endpoint_by_query',
38+
'find_endpoints',
39+
'get_global_metadata',
40+
)
41+
42+
__version__ = '0.2.0'

python/mesc/interface.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
from __future__ import annotations
22

33
import os
4-
from typing_extensions import Any, Mapping, Sequence
4+
import typing
55

6-
from .types import mesc_env_vars, Endpoint, RpcConfig
6+
from .types import mesc_env_vars
77
from . import network_utils
88
from . import load
99

10+
if typing.TYPE_CHECKING:
11+
from typing_extensions import Any, Mapping, Sequence
12+
from .types import Endpoint, RpcConfig
13+
1014

1115
def is_mesc_enabled() -> bool:
1216
"""MESC is enabled if two criteria are met:

python/mesc/load.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
from __future__ import annotations
2+
23
import json
34
import os
4-
from typing_extensions import cast, overload, Literal, Any
5+
import typing
56

6-
from .types import RpcConfig
77
from . import exceptions
88
from . import overrides
99
from . import validation
1010

11+
if typing.TYPE_CHECKING:
12+
from typing_extensions import cast, Literal, Any
13+
from .types import RpcConfig
14+
1115

1216
def read_config_data() -> RpcConfig:
1317
"""read MESC config according to MESC environment variables"""
@@ -40,12 +44,12 @@ def read_config_data() -> RpcConfig:
4044
return cast(RpcConfig, config)
4145

4246

43-
@overload
47+
@typing.overload
4448
def read_env_config(*, validate: Literal[False]) -> Any:
4549
...
4650

4751

48-
@overload
52+
@typing.overload
4953
def read_env_config(*, validate: Literal[True]) -> RpcConfig:
5054
...
5155

@@ -73,12 +77,12 @@ def read_env_config(*, validate: bool = True) -> RpcConfig | Any:
7377
return config
7478

7579

76-
@overload
80+
@typing.overload
7781
def read_file_config(*, path: str | None = None, validate: Literal[False]) -> Any:
7882
...
7983

8084

81-
@overload
85+
@typing.overload
8286
def read_file_config(*, path: str | None = None, validate: Literal[True]) -> RpcConfig:
8387
...
8488

python/mesc/network_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from __future__ import annotations
22

33
import typing
4-
from .types import RpcConfig
54
from . import network_names
65

6+
if typing.TYPE_CHECKING:
7+
from .types import RpcConfig
8+
79

810
def is_chain_id(chain_id: str) -> bool:
911
"""return True if input is a valid chain_id"""

python/mesc/overrides.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
import json
55
import os
66
import re
7-
from typing_extensions import Any, Mapping, MutableMapping
7+
import typing
88

99
from . import interface, network_utils, exceptions
10-
from .types import RpcConfig, Endpoint, Profile
10+
11+
if typing.TYPE_CHECKING:
12+
from typing_extensions import Any, Mapping, MutableMapping
13+
from .types import RpcConfig, Endpoint, Profile
1114

1215

1316
def apply_env_overrides(config: RpcConfig | None) -> RpcConfig:

python/mesc/types.py

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from __future__ import annotations
22

3-
from typing_extensions import Any, MutableMapping, TypedDict, Literal
3+
import typing
4+
5+
if typing.TYPE_CHECKING:
6+
from typing_extensions import Any, MutableMapping, TypedDict, Literal
47

58

69
mesc_env_vars = [
@@ -17,29 +20,29 @@
1720
]
1821

1922

20-
class Endpoint(TypedDict):
21-
name: str
22-
url: str
23-
chain_id: str | None
24-
endpoint_metadata: MutableMapping[str, Any]
25-
23+
if typing.TYPE_CHECKING:
2624

27-
class Profile(TypedDict):
28-
name: str
29-
default_endpoint: str | None
30-
network_defaults: MutableMapping[str, str]
31-
profile_metadata: MutableMapping[str, Any]
32-
use_mesc: bool
25+
class Endpoint(TypedDict):
26+
name: str
27+
url: str
28+
chain_id: str | None
29+
endpoint_metadata: MutableMapping[str, Any]
3330

31+
class Profile(TypedDict):
32+
name: str
33+
default_endpoint: str | None
34+
network_defaults: MutableMapping[str, str]
35+
profile_metadata: MutableMapping[str, Any]
36+
use_mesc: bool
3437

35-
class RpcConfig(TypedDict):
36-
mesc_version: str
37-
default_endpoint: str | None
38-
endpoints: MutableMapping[str, Endpoint]
39-
network_defaults: MutableMapping[str, str]
40-
network_names: MutableMapping[str, str]
41-
profiles: MutableMapping[str, Profile]
42-
global_metadata: MutableMapping[str, Any]
38+
class RpcConfig(TypedDict):
39+
mesc_version: str
40+
default_endpoint: str | None
41+
endpoints: MutableMapping[str, Endpoint]
42+
network_defaults: MutableMapping[str, str]
43+
network_names: MutableMapping[str, str]
44+
profiles: MutableMapping[str, Profile]
45+
global_metadata: MutableMapping[str, Any]
4346

4447

4548
endpoint_types: dict[str, type | tuple[type, ...]] = {
@@ -71,61 +74,58 @@ class RpcConfig(TypedDict):
7174
# # query types
7275
#
7376

74-
75-
class EndpointQuery(TypedDict):
76-
query_type: Literal[
77-
'default_endpoint',
78-
'endpoint_by_name',
79-
'endpoint_by_network',
80-
'user_input',
81-
]
82-
fields: (
83-
DefaultEndpointQuery | EndpointNameQuery | EndpointNetworkQuery | UserInputQuery
84-
)
85-
86-
87-
class DefaultEndpointQuery(TypedDict):
88-
profile: str | None
89-
90-
91-
class EndpointNameQuery(TypedDict):
92-
name: str
93-
94-
95-
class EndpointNetworkQuery(TypedDict):
96-
profile: str | None
97-
chain_id: str | int
98-
99-
100-
class UserInputQuery(TypedDict):
101-
profile: str | None
102-
user_input: str
103-
104-
105-
class MultiEndpointQuery(TypedDict, total=False):
106-
name_contains: str | None
107-
url_contains: str | None
108-
chain_id: str | int | None
109-
110-
111-
class GlobalMetadataQuery(TypedDict, total=False):
112-
profile: str | None
113-
114-
115-
class MescQuery(TypedDict):
116-
query_type: Literal[
117-
'default_endpoint',
118-
'endpoint_by_name',
119-
'endpoint_by_network',
120-
'user_input',
121-
'multi_endpoint',
122-
'global_metadata',
123-
]
124-
fields: (
125-
DefaultEndpointQuery
126-
| EndpointNameQuery
127-
| EndpointNetworkQuery
128-
| UserInputQuery
129-
| MultiEndpointQuery
130-
| GlobalMetadataQuery
131-
)
77+
if typing.TYPE_CHECKING:
78+
79+
class EndpointQuery(TypedDict):
80+
query_type: Literal[
81+
'default_endpoint',
82+
'endpoint_by_name',
83+
'endpoint_by_network',
84+
'user_input',
85+
]
86+
fields: (
87+
DefaultEndpointQuery
88+
| EndpointNameQuery
89+
| EndpointNetworkQuery
90+
| UserInputQuery
91+
)
92+
93+
class DefaultEndpointQuery(TypedDict):
94+
profile: str | None
95+
96+
class EndpointNameQuery(TypedDict):
97+
name: str
98+
99+
class EndpointNetworkQuery(TypedDict):
100+
profile: str | None
101+
chain_id: str | int
102+
103+
class UserInputQuery(TypedDict):
104+
profile: str | None
105+
user_input: str
106+
107+
class MultiEndpointQuery(TypedDict, total=False):
108+
name_contains: str | None
109+
url_contains: str | None
110+
chain_id: str | int | None
111+
112+
class GlobalMetadataQuery(TypedDict, total=False):
113+
profile: str | None
114+
115+
class MescQuery(TypedDict):
116+
query_type: Literal[
117+
'default_endpoint',
118+
'endpoint_by_name',
119+
'endpoint_by_network',
120+
'user_input',
121+
'multi_endpoint',
122+
'global_metadata',
123+
]
124+
fields: (
125+
DefaultEndpointQuery
126+
| EndpointNameQuery
127+
| EndpointNetworkQuery
128+
| UserInputQuery
129+
| MultiEndpointQuery
130+
| GlobalMetadataQuery
131+
)

python/mesc/validation.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from __future__ import annotations
22

3-
from typing_extensions import Any
4-
from typing import Sequence
3+
import typing
54

65
from .exceptions import InvalidConfig
76
from .types import rpc_config_types, endpoint_types, profile_types
87
from . import network_utils
98

9+
if typing.TYPE_CHECKING:
10+
from typing import Sequence
11+
from typing_extensions import Any
12+
1013

1114
def is_valid(config: Any) -> bool:
1215
try:

tests/generate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def generate_tests() -> list[Test]:
235235
new_test = copy.deepcopy(test)
236236
new_test[3]["fields"]["profile"] = "not_using_mesc" # type: ignore
237237
if query["query_type"] == "global_metadata":
238-
expected: dict | None = {}
238+
expected: dict[str, Any] | None = {}
239239
else:
240240
expected = None
241241
new_test = new_test[0:4] + (expected,) + new_test[5:]

0 commit comments

Comments
 (0)