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
This library is designed to solve one of the most important problems in python programming - dividing all written code into 2 camps: sync and async. We get rid of code duplication by using templates.
15
20
16
21
@@ -21,6 +26,7 @@ This library is designed to solve one of the most important problems in python p
21
26
-[**Code generation**](#code-generation)
22
27
-[**Markers**](#markers)
23
28
-[**Superfunctions**](#superfunctions)
29
+
-[**Typing**](#typing)
24
30
25
31
26
32
## Quick start
@@ -297,3 +303,34 @@ However, it is not completely free. The fact is that this mode uses a special tr
297
303
- Exceptions will not work normally inside this function. Rather, they can be picked up and intercepted in [`sys.unraisablehook`](https://docs.python.org/3/library/sys.html#sys.unraisablehook), but they will not go up the stack above this function. This is due to a feature of CPython: exceptions that occur inside callbacks for finalizing objects are completely escaped.
298
304
299
305
This mode is well suited for functions such as logging or sending statistics from your code: simple functions from which no exceptions or return values are expected. In all other cases, I recommend using the tilde syntax.
306
+
307
+
308
+
## Typing
309
+
310
+
Typing is the most difficult problem we faced when developing this library. In most situations, it has already been solved, but in some cases you may still notice flaws when using `mypy` or other static type analyzers. If you encounter similar problems, please [report](https://github.com/pomponchik/transfunctions/issues) them.
311
+
312
+
There are 2 main difficulties in developing typing here:
313
+
314
+
- Code generation creates code in runtime that is not in the source files of your project. Whereas most type analyzers look at your code statically, at what is actually present in your files.
315
+
- We mix several types of syntax in a single template function, but the static analyzer does not know that this is a template and part of the code will be deleted from here. In its opinion, this is the final function that will continue to be used in your project.
316
+
317
+
As you can see, typing in Python is not well suited for metaprogramming. However, in this project, almost all the problems with typing turned out to be solved in one way or another. The main reason why this is so is that we mostly *remove* code from functions, but hardly *add* it there during code generation. In other words, we almost never encounter the problem of how to type the *added* code. This makes the solution to most typing problems accessible. However! Unfortunately, we were not able to completely hide all the typing problems under the hood, but you should still be aware of some of them if you use `mypy` or another analyzer.
318
+
319
+
If you use the keyword `yield from`, you need to call the function `yield_from_it` instead:
320
+
321
+
```python
322
+
from transfunctions import yield_it
323
+
324
+
@superfunction
325
+
defmy_superfunction():
326
+
print('so, ', end='')
327
+
with sync_context:
328
+
print("it's just usual function!")
329
+
with async_context:
330
+
print("it's an async function!")
331
+
with generator_context:
332
+
print("it's a generator function!")
333
+
yield_from_it([1, 2, 3])
334
+
```
335
+
336
+
The keywords yield or yield from are available to you and work perfectly, but from the point of view of a static type checker, they turn the function into a generator, which should also mean a special type annotation. By replacing this fragment with a function call, we hack it.
0 commit comments