Comments (8)
In this case, the docs should be updated. Thanks for filing the issue
from dry-types.
Can you point out where you found this class definition?
class User
def initialize(name:)
@name = name
end
end
from dry-types.
Can you point out where you found this class definition?
class User def initialize(name:) @name = name end end
The docs don't have it there but based on the example here, I think it is the User definition.
from dry-types.
I did it as a temporary fix:
module Types
include Dry::Types(default: :strict)
def self.Constructor(klass)
# DryTypes Constructor doesn't support Ruby 3.1 initialization
# this is temporary code to make it work.
#
# See more details at: https://github.com/dry-rb/dry-types/issues/459
super { |item| klass.new(**item) }
end
end
from dry-types.
The docs don't have it there but based on the example here, I think it is the User definition.
This assumption is wrong, Types.Constructor
expects sequential arguments. The correct definition is
class User
def initialize(attributes)
@attributes = attributes
end
def name = @attributes.fetch(:name)
end
We should update the docs with a sample User
definition.
from dry-types.
I did it as a temporary fix:
I would suggest using a different name
module Types
include Dry::Types(default: :strict)
def self.KwConstructor(klass)
# DryTypes Constructor doesn't support Ruby 3.1 initialization
# this is temporary code to make it work.
#
# See more details at: https://github.com/dry-rb/dry-types/issues/459
super { |item| klass.new(**item) }
end
end
but it's up to you.
from dry-types.
The docs don't have it there but based on the example here, I think it is the User definition.
This assumption is wrong,
Types.Constructor
expects sequential arguments. The correct definition isclass User def initialize(attributes) @attributes = attributes end def name = @attributes.fetch(:name) endWe should update the docs with a sample
User
definition.
Hi @flash-gordon,
Thank you for your attention,
My case wasn't related to this User example, this example I copied from the original reported issue here
My case was using Dry Initializer like:
class User
extend Dry::Initializer[undefined: false]
option :id, type: Types::Integer
end
class Delete
extend Dry::Initializer[undefined: false]
option :user, type: Types.Constructor(::User)
end
# and using it like:
Delete.new(user: { id: 1 })
# `block in __dry_initializer_initialize__': User: option 'id' is required (KeyError)
So using only Dry Gems and only in Ruby 3.1+, I'm having this issue, what is weird. It seems like a bug. Since everyone using Ruby before 3.1 on this way, will have this issue when upgrading to Ruby 3.1
from dry-types.
It's because dry-initializer uses keywords. Before 3.0 ruby was more permissive regarding passing hashes where keywords are expected. Once a clear separation was made in 3.0 it doesn't work magically anymore. I cannot see what dry-types or dry-initializer can/should do about that. Defining a custom constructor is a consistent and non-breaking solution.
To be honest, I'm trying to stay out of the subject, it is very-very-very likely if we dig deeper by trying to fix anything here we'll come up with more bugs. Also, performance may suffer.
from dry-types.
Related Issues (20)
- Types::Params::*.optional not handling empty string HOT 4
- Anonymous Dry::Types based module cannot be extended HOT 4
- Params namespace disapearing HOT 9
- Params coercion for Array works in an unintuitive way HOT 1
- https://dry-rb.org/gems/dry-types/ not at latest version HOT 1
- [Security] Workflow ci.yml is using vulnerable action actions/checkout
- Compound Types with Examples HOT 1
- Dry::Container::Error: There is already an item registered with the key "nominal.string" on jruby-head HOT 4
- Future-proof local override for Types::Instance/Constructor classes that potentially collide with predicates
- Unexpected error raised from Sum type HOT 7
- Types::Coeercible::Integer, should this use Base 10? HOT 2
- Add Zeitwerk autoloader
- Using transform_keys breaks error handling
- Undefined method after upgrading dependencies. HOT 5
- Missing method implementation for `Enum.each_value` HOT 1
- A bug with array, struct and sum of structs in `try` method
- Fix BigDemical warning
- default causes try/1 to raise an ConstraintError exception rather than an Failure
- Dry-Types Gem Issue with Coercing Float Strings to Integers 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 dry-types.