question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extend `SelfAttribute` with some `callback` kwarg, for convenience

See original GitHub issue

The 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 the LazyAttribute 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:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
francoisfreitagcommented, Sep 17, 2022

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!

1reaction
francoisfreitagcommented, Aug 26, 2022

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).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found