Comments (2)
Makes sense to me! I'm all in favor.
Are there any other places where a type change like this would also make sense/make things easier?
from pydash.
Not really type changes but improvement ideas I had:
There is the sort_by
min_by
max_by
functions that don't support accessing the key of a mapping in the iteratee. i think it would be cool if we could. I had this use case recently and I had to go out of the chain to use builtins min
, max
instead.
Also one thing that would be cool to have in the library is a maybe apply function kinda thing. pydash.get
is really cool but I am a bit sad that it is not typable in the current python type system. One very common use case of it I believe is to get data from an optional value:
import pydash as _
class SomeClass:
attribute: int = 5
@classmethod
def build(cls) -> "SomeClass | None":
...
some_class = SomeClass.build()
attr = _.get(some_class, "attribute") # `attr` is `Any`, we cannot type `get` properly
But we could have a maybe_apply
function thing that would take a callable:
attr = _.maybe_apply(some_class, lambda x: x.attribute) # `attr` is `int | None`
this is typable.
And it is not restricted to attribute or key getting, we can just apply anything to an optional value, it abstracts this pattern:
def add1(x: int) -> int:
return x + 1
some_int: Optional[int]
if some_int is not None:
some_int = add1(some_int)
# instead just do
some_int = _.maybe_apply(some_int, add1)
And with the chaining interface I think it would look really cool, eg:
import pydash as _
from dataclasses import dataclass
@dataclass
class SomeAddress:
city: str | None
@dataclass
class SomeUser:
addr: SomeAddress | None
@dataclass
class SomeClass:
user: SomeUser | None
some_class: SomeClass
maybe_upper_city: str | None = (
_.chain(some_class)
.maybe_apply(lambda x: x.user)
.maybe_apply(lambda x: x.addr)
.maybe_apply(lambda x: x.city)
.maybe_apply(lambda x: x.upper())
)
from pydash.
Related Issues (20)
- pytest_mypy_testing tests are very slow HOT 2
- slugify behavior with single quotes HOT 1
- No module named 'typing_extensions' in pydash 7.0.0 HOT 1
- Missing dependency "typing_extensions" HOT 1
- Incompatible with typing-extensions 4.6 HOT 3
- how to use capture group in reg_exp_js_replace? HOT 1
- Typing: find_index only accepts Callable or None HOT 3
- Zip type hinting and starmap HOT 3
- Take while does not support generators
- Support for multithreading. HOT 1
- Typing call-overload error HOT 5
- Map keys is broken HOT 3
- max_by should return max value, not object HOT 3
- Wrong assumption of function argcount when using a negated lambda assigned as a class attribute
- Documentation: pydash-cheatsheet
- Type overloading for get
- Unexpected difference in path behavior for set_() when accessing attribute of object
- multiply does not math
- Use ruff HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pydash.