You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+35-2
Original file line number
Diff line number
Diff line change
@@ -24,12 +24,13 @@ Based features include:
24
24
- Support for `Intersection` types
25
25
- Default return type of `None` instead of `Any`
26
26
- Generic `TypeVar` bounds
27
+
- Based type-guards
27
28
- Infer parameter type from default value
28
29
- Infer overload types
29
30
- Bare literals
30
31
- Tuple literal types
31
32
32
-
See the [features](#features) for a more information.
33
+
See the [features](#features) for more information, or [the docs](https://kotlinisland.github.io/basedmypy/based_features.html) for a comprehensive list.
33
34
34
35
## Usage
35
36
@@ -49,10 +50,13 @@ Basedmypy is installed as an alternative to, and in place of, the `mypy` install
49
50
50
51
## Features
51
52
52
-
Have you ever tried to use Pythons type system and thought to yourself "This doesn't seem based"?
53
+
Have you ever tried to use Python's type system and thought to yourself "This doesn't seem based"?
53
54
54
55
Well fret no longer as basedmypy has got you covered!
55
56
57
+
You can find a comprehensive list in [the docs](https://kotlinisland.github.io/basedmypy/based_features.html).
58
+
59
+
56
60
### Baseline
57
61
58
62
Basedmypy has baseline, baseline is based! It allows you to adopt new strictness or features
@@ -145,6 +149,35 @@ reveal_type(foo(["based"], "mypy")) # N: Revealed type is "list[str]"
145
149
reveal_type(foo({1, 2}, 3)) # N: Revealed type is "set[int]"
146
150
```
147
151
152
+
### Based type-guards
153
+
154
+
Type-guards have been re-designed from the ground up:
155
+
156
+
```py
157
+
# The target parameter of the typeguard can be specified
158
+
defguard(name: str, x: object) -> x isint: ...
159
+
160
+
# impossible type-guards show an error
161
+
defbad(x: str) -> x isint: ...# error: A type-guard's type must be assignable to its parameter's type. (guard has type "int", parameter has type "str")
162
+
163
+
classA: ...
164
+
classB: ...
165
+
defis_b(x: object) -> x is B: ...
166
+
167
+
x = A()
168
+
assert is_b(x)
169
+
# type-guards narrow instead of resetting the type
170
+
reveal_type(x) # A & B
171
+
172
+
# type-guards work on instance parameters
173
+
classFoo:
174
+
defguard(self) -> selfisint: ...
175
+
176
+
f = Foo()
177
+
assert f.guard()
178
+
reveal_type(f) # Foo & int
179
+
```
180
+
148
181
### Overload Implementation Inference
149
182
150
183
The types in overload implementations (including properties) can be inferred:
0 commit comments