Skip to content

Commit 2fa9fda

Browse files
Adrian AcalaAdrian Acala
authored andcommitted
feat: Implement __replace__ method in BaseContainer for copy.replace() support
- Added the __replace__ magic method to BaseContainer, enabling the creation of modified copies of immutable containers in line with Python 3.13's copy.replace() functionality. - Updated documentation to include usage examples and clarify the behavior of the new method. - Added tests to ensure the correct functionality of the __replace__ method and its integration with the copy module. - Updated CHANGELOG to reflect this new feature and its implications for container usage. Closes #1920.
1 parent ac1bf89 commit 2fa9fda

File tree

4 files changed

+368
-339
lines changed

4 files changed

+368
-339
lines changed

CHANGELOG.md

Lines changed: 4 additions & 335 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ See [0Ver](https://0ver.org/).
1313
- A lot of new features added to our `hypothesis` plugins.
1414
It now allows to create custom strategies, better instantiate containers,
1515
docouple it from built-in ones, check laws from user-defined interfaces
16+
- Add support for `copy.replace()` from Python 3.13+ by implementing `__replace__`
17+
magic method on `BaseContainer`. This allows for creating modified copies
18+
of immutable containers. (#1920)
1619

1720
### Bugfixes
1821

@@ -321,338 +324,4 @@ New Year Release! 🎄
321324
`FutureResult` and `RequiresContextFutureResult`
322325
- Adds `__copy__` and `__deepcopy__` magic methods to `Immutable` class
323326
- Speeds up ``is_successful`` function
324-
- Makes all `Context` context helpers abstract,
325-
so you cannot create new instances of this class,
326-
also adds `__slots__` to these classes
327-
- Improves `RequiresContext*` types with `NoDeps` where it is logically true
328-
329-
### Bugfixes
330-
331-
- Fixes that `@safe` decorator was generating incorrect signatures
332-
for functions with `Any`
333-
- Fixes that `.rescue()` of `RequiresContextResult` was returning `Any`
334-
- Fixes that `.rescue()` of `RequiresContextIOResult` was returning `Any`
335-
- Fixes that `RequiresContextResult` and `RequiresContextIOResult`
336-
were not `final`
337-
- Fixes that `ImmutableStateError` was not a subclass of `AttributeError`
338-
- Fixes that `IOResult` was not showing `str` representation
339-
of wrapped `inner_value`
340-
341-
### Misc
342-
343-
- Replaces `pytest-asyncio` with `anyio` plugin,
344-
now we test compatibility with any IO stack: `asyncio`, `trio`, `curio`
345-
- Updates lots of dependencies
346-
- Adds lots of new tests
347-
- Updates lots of docs
348-
- Removes "IO marker" name from docs in favor for "IO container",
349-
it is not special at all. Why would we call it differently?
350-
351-
352-
## 0.13.0
353-
354-
### Features
355-
356-
- **Breaking**: renames `join` to `flatten`, sorry!
357-
- **Breaking**: renames `box` to `bind` and moves it to `returns.pointfree`
358-
- **Breaking**: removes `Maybe.rescue` and `Maybe.fix` methods
359-
- **Breaking**: renames `io_squash` to `squash_io`
360-
and moves it to `returns.converters`
361-
- **Breaking**: moves all interfaces from `returns.primitives.container` to
362-
`returns.primitives.interfaces`
363-
364-
- Adds `rescue` pointfree function
365-
- Adds `ResultE` alias for `Result[..., Exception]`
366-
367-
- Adds `RequiresContext` container and `Context` helper class
368-
- Adds `RequiresContext` support for `bind` pointfree function
369-
- Adds `RequiresContext` support for `flatten` function
370-
371-
- Adds `RequiresContextResult` container
372-
- Adds `RequiresContextResultE` alias
373-
- Adds `ReaderResult` and `ReaderResultE` aliases
374-
for `RequiresContextResult[..., ..., Exception]`
375-
- Adds `RequiresContextResult` support for `bind` and `rescue`
376-
- Adds `RequiresContextResult` support for `flatten`
377-
378-
- Adds `IOResult` helper to work better with `IO[Result[a, b]]`
379-
- Adds `IOResultE` alias for `IOResult[a, Exception]`
380-
- Adds `IOResult` support for `bind`
381-
- Adds `IOResult` support for `flatten`
382-
- Adds `IOResult` support for `@pipeline`
383-
- Adds `IOResult` support for `coalesce`
384-
- Adds `IOResult` support for `is_successful`
385-
386-
- Adds `RequiresContextIOResult` container
387-
- Adds `RequiresContextIOResultE` alias
388-
- Adds `ReaderIOResult` and `ReaderIOResultE` aliases
389-
for `RequiresContextIOResult[..., ..., Exception]`
390-
- Adds `RequiresContextIOResult` support for `bind` and `rescue`
391-
- Adds `RequiresContextIOResult` support for `flatten`
392-
393-
- Adds `Result.lift`, `Maybe.lift`, `RequiresContext.lift`,
394-
and `RequiresContextResult.lift` functions in addition to `IO.lift`
395-
396-
- Adds `Immutable` primitive type
397-
- Adds `Unitable` protocol and `.from_success()` and `.from_failure()`
398-
methods for all `Result` related classes
399-
- Adds `Instanceable` protocol and `.from_value()` method
400-
for `IO` and `RequiresContext`
401-
402-
- Adds `flow` function, which is similar to `pipe`
403-
- Adds `swap` converter for `Result` and `IOResult`
404-
- Adds `squash_context` function to squash `RequiresContext` similar to `IO`
405-
406-
### Bugfixes
407-
408-
- Now `Success` and `Failure` (both `io` and pure) return `Any` and not `NoReturn`
409-
- Fixes how `flatten` works, also adds more tests and docs about `Failure` case
410-
- Fixes `Unwrappable` type being parametrized with only one `TypeVar`
411-
- Changes `Success` and `Failure` to return `Any` instead of `NoReturn`
412-
413-
### Misc
414-
415-
- Updates `poetry` version in `travis`
416-
- Improves ``pipe`` docs with ``lambda`` and `Generic` problem
417-
- Improves docs in several places
418-
- Now examples in docs tries to be docstests where possible
419-
- Changes how tests are checked with `mypy` in CI
420-
421-
422-
## 0.12.0
423-
424-
### Features
425-
426-
- **Breaking**: now `@pipeline` requires a container type when created:
427-
`@pipeline(Result)` or `@pipeline(Maybe)`
428-
- `Maybe` and `Result` now has `success_type` and `failure_type` aliases
429-
- Adds `Result.unify` utility method for better error type composition
430-
- We now support `dry-python/classes` as a first-class citizen
431-
- Adds `io_squash` to squash several `IO` containers into one container
432-
with a tuple inside, currently works with `9` containers max at a time
433-
- Adds `untap` function which does convert return type to `None`
434-
435-
### Bugfixes
436-
437-
- Fixes that containers were not usable with `multiprocessing`
438-
- Changes the inheritance order, now `BaseContainer` is the first child
439-
- Fixes that `Nothing` had incorrect docstrings
440-
441-
### Misc
442-
443-
- Now `generated` package is protected
444-
- Updates `poetry` to `1.0`
445-
446-
447-
## 0.11.0
448-
449-
### Features
450-
451-
- **Breaking**: now `pipe()` does not require argument to be the first value,
452-
instead it is required to use: `pipe(f1, f2, f3, f4)(value)`
453-
- **Breaking**: dropped everything from `returns/__init__.py`,
454-
because we now have quite a lot of stuff
455-
- **Breaking**: dropped support of zero argument functions for `Nothing.fix`
456-
- **Breaking**: dropped support of zero argument functions for `Nothing.rescue`
457-
- `Maybe` now has `.failure()` to match the same API as `Result`
458-
- Adds `identity` function
459-
- Adds `tap` function
460-
- Now `pipe` allows to pipe 8 steps
461-
- Adds `coalesce_result` and `coalesce_maybe` converters
462-
463-
### Bugfixes
464-
465-
- Fixes that code inside `.fix` and `.rescue` of `Maybe` might be called twice
466-
467-
### Misc
468-
469-
- Now all methods have doctests
470-
- Updates docs about `Success` and `_Success`, `Failure` and `_Failure`
471-
- Updates docs about `@pipeline`
472-
- Typechecks async functions and decorators inside `typesafety/` tests
473-
474-
475-
## 0.10.0
476-
477-
### Features
478-
479-
- **Breaking**: `python>=3.7,<=3.7.2` are not supported anymore,
480-
because of a bug inside `typing` module
481-
- **Breaking**: Now `bind` does not change the type of an error
482-
- **Breaking**: Now `rescue` does not change the type of a value
483-
- **Breaking**: Renames `map_failure` to `alt`
484-
- Adds `box()` function with the ability
485-
to box function for direct container composition like:
486-
`a -> Container[b]` to `Container[a] -> Container[b]`
487-
- Adds `IO.lift()` function to lift `a -> a` to `IO[a] -> IO[a]`
488-
- Adds `pipe()` function to `pipeline.py`
489-
- Adds `__hash__()` magic methods to all containers
490-
491-
### Bugfixes
492-
493-
- Changes `Any` to `NoReturn` in `Success` and `Failure`
494-
- Now all type parameters in `Result`, `Maybe`, and `IO` are covariant
495-
496-
### Misc
497-
498-
- Massive docs rewrite
499-
- Updates `mypy` version
500-
- Updates `wemake-python-styleguide` and introduces `nitpick`
501-
- Updates `pytest-plugin-mypy`, all tests now use `yml`
502-
503-
504-
## 0.9.0
505-
506-
### Features
507-
508-
- Provides a bunch of primitive interfaces to write your own containers
509-
- Adds `.map_failure()` method
510-
- Adds `flatten()` function to join nested containers
511-
512-
### Bugfixes
513-
514-
- Fixes type of `Maybe.fix` and `Maybe.rescue` to work with both `lambda: 1` and `lambda _: 1`
515-
516-
### Misc
517-
518-
- Improves `README`
519-
520-
521-
## 0.8.0
522-
523-
### Features
524-
525-
- Reintroduces the `Maybe` container, typed!
526-
- Introduces converters from one type to another
527-
- Adds `mypy` plugin to type decorators
528-
- Complete rewrite of `Result` types
529-
- Partial API change, now `Success` and `Failure` are not types, but functions
530-
- New internal types introduced: `FixableContainer` and `ValueUnwrapContainer`
531-
532-
### Bugfixes
533-
534-
- Fixes issue when you could return `IO` container from `Result.bind`
535-
- Fixes `@pipeline` return type
536-
537-
### Misc
538-
539-
- Reapplied all types to `.py` files
540-
- Improved docs about `IO` and `Container` concept
541-
- Adds docs about container composition
542-
- Moves from `Alpha` to `Beta`
543-
544-
545-
## 0.7.0
546-
547-
### Features
548-
549-
- Adds `IO` container
550-
- Adds `unsafe` module with unsafe functions
551-
- Changes how functions are located inside the project
552-
553-
### Bugfixes
554-
555-
- Fixes container type in `@pipeline`
556-
- Now `is_successful` is public
557-
- Now `raise_exception` is public
558-
559-
### Misc
560-
561-
- Changes how `str()` function works for container types
562-
- Total rename to "container" in the source code
563-
564-
565-
## Version 0.6.0
566-
567-
### Features
568-
569-
- `safe` and `pipeline` now supports `asyncio`
570-
- `is_successful` now returns `Literal` types if possible
571-
572-
573-
## Version 0.5.0
574-
575-
### Features
576-
577-
- Adds `compose` helper function
578-
- Adds public API to `import returns`
579-
- Adds `raise_exception` helper function
580-
- Adds full traceback to `.unwrap()`
581-
582-
583-
### Misc
584-
585-
- Updates multiple dev-dependencies, including `mypy`
586-
- Now search in the docs is working again
587-
- Relicenses this project to `BSD`
588-
- Fixes copyright notice in the docs
589-
590-
591-
## Version 0.4.0 aka Goodbye, containers!
592-
593-
### Features
594-
595-
- Moves all types to `.pyi` files
596-
- Renames all classes according to new naming pattern
597-
- **HUGE** improvement of types
598-
- Renames `fmap` to `map`
599-
- Renames `do_notation` to `pipeline`, moves it to `functions.py`
600-
- Renames `ebind` to `rescue`
601-
- Renames `efmap` to `fix`
602-
- Renames `container` to `Container`
603-
- Removes `Maybe` container, since typing does not have `NonNullable` type
604-
605-
606-
## Version 0.3.1
607-
608-
### Bugfixes
609-
610-
- Adds `py.typed` file to be `PEP561` compatible
611-
612-
613-
## Version 0.3.0, Renamed to `returns`
614-
615-
The project is renamed to `returns` and moved to `dry-python` org.
616-
617-
### Features
618-
619-
- Adds `.pyi` files for all modules,
620-
to enable `mypy` support for 3rd party users
621-
622-
623-
## Version 0.2.0
624-
625-
### Features
626-
627-
- Adds `Maybe` container
628-
- Adds immutability and `__slots__` to all containers
629-
- Adds methods to work with failures
630-
- Adds `safe` decorator to convert exceptions to `Result` container
631-
- Adds `is_successful()` function to detect if your result is a success
632-
- Adds `failure()` method to unwrap values from failed containers
633-
634-
### Bugfixes
635-
636-
- Changes the type of `.bind` method for `Success` container
637-
- Changes how equality works, so now `Failure(1) != Success(1)`
638-
- Changes how new instances created on unused methods
639-
640-
### Misc
641-
642-
- Improves docs
643-
644-
645-
## Version 0.1.1
646-
647-
### Bugfixes
648-
649-
- Changes how `PyPI` renders package's page
650-
651-
### Misc
652-
653-
- Improves `README` with new badges and installation steps
654-
655-
656-
## Version 0.1.0
657-
658-
Initial release. Featuring only `Result` and `do_notation`.
327+
- Makes all `Context`

0 commit comments

Comments
 (0)