Closed jmpcm closed 1 year ago
@jmpcm first of all thanks for this contribution
I have honestly never tried using __future__
so I'm gonna have to trust you on this one. As long as all existing tests still pass then I don't see any reason to not merge this in
This will also force me to learn about __future__
😄
Any objections from your side @marksmayo?
@christianhelle You're welcome! This is a very interesting library. My needs are the same of what you describe in your blog post. Really hope this project keeps growing ;)
I forgot to write the case that started it all :sweat_smile: Usually I use annotations
when 1) I want to write the return type in a class method, or 2) if there is a forward declaration of a class (I try to avoid this last case). The case that started it all was 1. Example:
from __future__ import annotations
from dataclasses import dataclass
from autofaker import Autodata
@dataclass
class Device:
id: int
type: str
battery: str
@classmethod
def from_json(cls, value:str) -> Device:
pass
If you don't import annotations
, the function definition will have to be written as def from_json(cls, value:str) -> "Device":
(note the double commas around Device
). This is because the type Device
is still not defined when the function is declared. the purpose of annotations (and it is something to be supported in the future) is to avoid placing the commas, because a type is, well, a type :)
@christianhelle You're welcome! This is a very interesting library. My needs are the same of what you describe in your blog post. Really hope this project keeps growing ;)
Thank you for your kind words and I'm glad that you find it interesting
Coming from a .NET background, when I started working with Python I immediately missed the unit testing tooling that I was used to so I had to build it myself. It was also a good learning exercise as I was very new to Python when I started on this
@jmpcm this contribution is now released to PyPi as v1.0.18
Great! Thank you 😄
Description
This PR adds support for retrieving the type name, when future.annotations is imported by a user. In the current version of the library, a string is always returned instead of a value of the annotated type. For example, for if the type of an attribute was an int, autofaker would return a string.
Steps to reproduce
The following example, based in the examples in README, can be used to test the PR:
Before this PR, the result could be something like
The result with the PR applied follows:
Discussion
I don't know if this is the best solution, but I couldn't find any other option. Feel free to propose any other option that might suit the library better.
I executed all tests and they are all still passing. Yet, I didn't added any new tests, since I don't know how to test this.
Adding support for
__future__.annotations
is important, since annotations was not integrated in Python 3.11 (initially is was expected to be integrated in Python 3.10) and there isn't any exception of when it will be [1]. Adding support, enables better experience for the user of the library.