Extend `SelfAttribute` with some `callback` kwarg, for convenience
See original GitHub issueThe problem
Quite a lot of times I’d like to refine a value that can be accessed with a SelfAttribute
. This can normally be done by combining it with a LazyAttribute
and 1 or 2 param variables, but it’d be quite convenient if that was a shortcut so one didn’t have to write that out explicitly every time. And also avoid having to either invent new variable names or prefix them to avoid collisions.
I also think, in general, that it would be a nice extension for SelfAttribute
to expose a way to intercept the picked up value before it’s assigned. A bit similar to what LazyAttribute
by itself does (though on the factory instance).
Consider this simple case:
class DataFactory(factory.DictFactory):
coordinates = factory.Dict(
{
"lat": factory.SelfAttribute("..latitude"),
"lng": factory.SelfAttribute("..longitude"),
}
)
class Params:
# Faker outputs latitude and longitude as `Decimal`, I want it as `float`
lat = factory.Faker("latitude")
lng = factory.Faker("longitude")
latitude = factory.LazyAttribute(lambda obj: float(obj.lat))
longitude = factory.LazyAttribute(lambda obj: float(obj.lng))
Which could then become
class DataFactory(factory.DictFactory):
coordinates = factory.DictFactory(
{
"lat": factory.SelfAttribute("..latitude", callback=float),
"lng": factory.SelfAttribute("..longitude", callback=float),
}
)
class Params:
latitude = factory.Faker("latitude")
longitude = factory.Faker("longitude")
Extra notes
- I’m aware that I could place and evaluate the
Faker
instance inside of theLazyAttribute
function, but it feels like this could be supported by the public API, rather than having to resort to a private API. - Better naming suggestions for the “callback” kwarg are welcome (one suggestion I have is
coerce
)
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top Results From Across the Web
factory_boy - Bountysource
But in some cases, when I ask for an existing object with an existing SubFactory ... Extend `SelfAttribute` with some `callback` kwarg, for...
Read more >Reference — Factory Boy stable documentation
A Factory 's behavior can be tuned through a few settings. For convenience, they are declared in a single class Meta attribute: class...
Read more >Move some extended callback functionality to the base ...
A better approach is to make all callbacks compatible with the extended format by providing a **kwargs argument. All reactions.
Read more >attrs by Example - attrs 22.2.0 documentation
But you'll usually want some data on your classes, so let's add some: ... For that, attrs.asdict() offers a callback that decides whether...
Read more >Use _convenienceMatchR in pyatom With Examples | LambdaTest
Learn how to use _convenienceMatchR function in pyatom framework for your next python automation project with LambdaTest Automation Testing Advisor.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Not much interest has been shown for this proposal, we won’t be moving forward with it in the short term. Thank you for taking the time to submit it, we’ll happily review new proposals in the future!
I’m not sold on the use case. Why not change the data source to generate the data type of interest? Here, that could mean extending Faker if the use case is general enough, or writing helper for your project that generates float longitude and lattitude (maybe from Faker Decimal values).