1
+ from __future__ import annotations
2
+
1
3
import os
2
4
import typing
3
5
from pathlib import Path
@@ -51,7 +53,7 @@ def __len__(self) -> int:
51
53
class Config :
52
54
def __init__ (
53
55
self ,
54
- env_file : typing . Optional [ typing . Union [ str , Path ]] = None ,
56
+ env_file : str | Path | None = None ,
55
57
environ : typing .Mapping [str , str ] = environ ,
56
58
env_prefix : str = "" ,
57
59
) -> None :
@@ -64,17 +66,15 @@ def __init__(
64
66
self .file_values = self ._read_file (env_file )
65
67
66
68
@typing .overload
67
- def __call__ (self , key : str , * , default : None ) -> typing . Optional [ str ] :
69
+ def __call__ (self , key : str , * , default : None ) -> str | None :
68
70
...
69
71
70
72
@typing .overload
71
- def __call__ (self , key : str , cast : typing . Type [T ], default : T = ...) -> T :
73
+ def __call__ (self , key : str , cast : type [T ], default : T = ...) -> T :
72
74
...
73
75
74
76
@typing .overload
75
- def __call__ (
76
- self , key : str , cast : typing .Type [str ] = ..., default : str = ...
77
- ) -> str :
77
+ def __call__ (self , key : str , cast : type [str ] = ..., default : str = ...) -> str :
78
78
...
79
79
80
80
@typing .overload
@@ -87,23 +87,21 @@ def __call__(
87
87
...
88
88
89
89
@typing .overload
90
- def __call__ (
91
- self , key : str , cast : typing .Type [str ] = ..., default : T = ...
92
- ) -> typing .Union [T , str ]:
90
+ def __call__ (self , key : str , cast : type [str ] = ..., default : T = ...) -> T | str :
93
91
...
94
92
95
93
def __call__ (
96
94
self ,
97
95
key : str ,
98
- cast : typing .Optional [ typing . Callable [[typing .Any ], typing .Any ]] = None ,
96
+ cast : typing .Callable [[typing .Any ], typing .Any ] | None = None ,
99
97
default : typing .Any = undefined ,
100
98
) -> typing .Any :
101
99
return self .get (key , cast , default )
102
100
103
101
def get (
104
102
self ,
105
103
key : str ,
106
- cast : typing .Optional [ typing . Callable [[typing .Any ], typing .Any ]] = None ,
104
+ cast : typing .Callable [[typing .Any ], typing .Any ] | None = None ,
107
105
default : typing .Any = undefined ,
108
106
) -> typing .Any :
109
107
key = self .env_prefix + key
@@ -117,7 +115,7 @@ def get(
117
115
return self ._perform_cast (key , default , cast )
118
116
raise KeyError (f"Config '{ key } ' is missing, and has no default." )
119
117
120
- def _read_file (self , file_name : typing . Union [ str , Path ] ) -> typing . Dict [str , str ]:
118
+ def _read_file (self , file_name : str | Path ) -> dict [str , str ]:
121
119
file_values : typing .Dict [str , str ] = {}
122
120
with open (file_name ) as input_file :
123
121
for line in input_file .readlines ():
@@ -133,7 +131,7 @@ def _perform_cast(
133
131
self ,
134
132
key : str ,
135
133
value : typing .Any ,
136
- cast : typing .Optional [ typing . Callable [[typing .Any ], typing .Any ]] = None ,
134
+ cast : typing .Callable [[typing .Any ], typing .Any ] | None = None ,
137
135
) -> typing .Any :
138
136
if cast is None or value is None :
139
137
return value
0 commit comments