With the typing_extensionsīackports you can use static typing features of the latest Python release inĪdding static typing to your code base makes it easier to read and more robust: Since Python 3.5 programmers have the option to add type annotations to their code along with tools like mypy to check that they are valid. The Python interpreter handles types in a dynamic and flexible way withoutĬonstraints on what type of object a variable is assigned to. I see that get_type_hints calls eval, so perhaps it makes no difference.This blog post is aimed at Python programmers who are interested in adding type Over-cautious, but I fear calling eval() on arbitrary strings, although I don’t have a good alternative to get_type_hints. The whole MRO of your class), and is too strict with its insistence that Looking more closely at get_type_hints, I suspect that it may notīe the right tool for the job you want. That automatically unstringifies the annotations on class creation: A: I think the Steering Council is, or was, lookingįor evidence that people are using annotations for purposes other thanīut if PEP 563 goes ahead, what you could do is write a class decorator I think that your use-case is a good argument in favour of reverting PEPĥ63 in favour of 649. So Iĭoubt there will be a choice between from _future_ import annotations # stringify annotationsįrom _past_ import annotations # don't stringify annotations Incompatible syntaxes, not to allow a choice between then forever. Of future imports is to manage the transition between two different, If you want to keep the current behaviour even if and when annotationsĪre automatically stringified, you probably won’t be able to. Put if PEP 563 will go on, it would be nice to have some way to opt out of it. If PEP 659 will pass through, it seems I will have no problems. Not so much in the future from _future_ import annotations # imitate PEP 563 This works great for me! Maybe some other creative soul will come out with other uses? Anyways, this is absolutely how it works now class A: The use case I have is allowing declarative notation for model description. Why I would like to do this is another question. Imagine I would like to have 42 in the annotations for A.y. Y: x = "and now for something completely different" I would like to retain the current behaviour, which new PEP may not support. Im aware of different PEPs related to the type hinting, this is why I’m writing my suggesting. Or some alternative to get_type_hints, which would run eval in the appropriate context, but would not required to return Type. So I suggest keeping the possibility to opt-out of new behaviors, maybe something like from _future_ import old_annotations The problem is, it look like future version of Python would not support such a frivolous use of annotations. I use it to run different modelling projects, including official COVID model for Finland. The library works very well (in my context), by bringing expressivity and brevity of declarative probabilistic programming where it is needed. Annotation describe prior for the model parameter, rather than its type (which is mostly irrelevant in my context). Here normal_prior would return a callable. It uses synaxis like class MyModel(Model): I had developed a library that allows to write declarative statements in Python, in the style of probabilistic programming. Maybe allow something like from _future_ import old_annotations With from _future_ import annotations it would return a string, which could be hard to parse. It is possible now, but I see it is going to be deprecated in the future.Ī: f() = "and now for something completely different"Īnd annotation for "a" would return a proper callable. I would like future versions of python to have some easy way to put anything into annotation, not just Types or stringified types.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |