Make Pydantic chuck error for wrong argument names - pydantic

Suppose I have the following class:
class ModelConfig(pydantic.BaseModel):
name: str = "bert"
If I were to instantiate it with model_config = ModelConfig(name2="hello"), this simply ignores that there is no name2 and just keeps name="bert". Is there a way to raise an error saying unknown argument in pydantic?

You can do this using the forbid Model Config
For example:
class ModelConfig(pydantic.BaseModel, extra=pydantic.Extra.forbid):
name: str = "bert"
Passing model_config = ModelConfig(name2="hello") will throw an error

Related

Error trying to get selected font from py QFontComboBox using activated.connect

(in PyQt5)
I am trying to get the name of the selected font from QFontComboBox to "showInfo" and an error occurs (detail after the code).
The code below is not part of a class.
Thanks!
def additional_menu_styled_font(editor):
projCombo = QFontComboBox
objectTest = QObject()
ShapeLayerList = QWidgetAction(objectTest)
ShapeLayerList.setDefaultWidget(projCombo())
popupMenu2A = QMenu()
popupMenu2A.setTitle("Shape Data")
popupMenu2A.addAction(ShapeLayerList)
popupMenu2A.exec_(QCursor.pos())
#fixme!!!
projCombo().activated.connect(changed)
Editor.additional_menu_styled_font = additional_menu_styled_font
def changed(projCombo):
showInfo(str(projCombo().currentFont().family()))
And the error:
...in additional_menu_styled_font
projCombo().activated.connect(changed)
TypeError: connect() failed between activated(int) and unislot()

ActiveJob::SerializationError: Unsupported argument type: Date/Class

I am getting a weird error while passing class and date to an ActiveJob with the Sidekiq adapter.
1] pry(main)> StripeTransactionsSyncJob.perform_later(Stripe::SyncCharges, nil, 3.days.ago.to_date)
ActiveJob::SerializationError: Unsupported argument type: Class
from /home/amit/.rvm/gems/ruby-2.5.8#immosite/gems/activejob-5.0.7.2/lib/active_job/arguments.rb:83:in `serialize_argument'
[2] pry(main)> StripeTransactionsSyncJob.perform_later('Stripe::SyncCharges', nil, 3.days.ago.to_date)
ActiveJob::SerializationError: Unsupported argument type: Date
from /home/amit/.rvm/gems/ruby-2.5.8#immosite/gems/activejob-5.0.7.2/lib/active_job/arguments.rb:83:in `serialize_argument'
As per the doc, ActiveJob should support both types of arguments out of the box. What is wrong here?
The guide you have referenced in your post refers to the v6.1.4 of Rails. See the version info on top-right corner on that page.
The guide for v5.0 doesn't explicitly specify about the arguments types supported. And looking at the source code (see below) for the version of Rails you are using i.e 5.0.7.2
def serialize_argument(argument)
case argument
when *TYPE_WHITELIST
argument
when GlobalID::Identification
convert_to_global_id_hash(argument)
when Array
argument.map { |arg| serialize_argument(arg) }
when ActiveSupport::HashWithIndifferentAccess
result = serialize_hash(argument)
result[WITH_INDIFFERENT_ACCESS_KEY] = serialize_argument(true)
result
when Hash
symbol_keys = argument.each_key.grep(Symbol).map(&:to_s)
result = serialize_hash(argument)
result[SYMBOL_KEYS_KEY] = symbol_keys
result
else
raise SerializationError.new("Unsupported argument type: #{argument.class.name}")
end
end
your passed argument types Class and Date are not supported and hence you are getting SerializationError.
Note: Whenever referring to the API-docs or Guide I would recommend to view them for the specific version of Rails you are using.
The Class/Date/DateTime/Time etc were not supported in Rails 5.0. So I need to use String form of data being passed to the Job.
For reference, here is the method(simplified) that does deserialization
def serialize_argument(argument)
case argument
when *[ NilClass, String, Integer, Float, BigDecimal, TrueClass, FalseClass ]
argument
when GlobalID::Identification
convert_to_global_id_hash(argument)
when Array
argument.map { |arg| serialize_argument(arg) }
when ActiveSupport::HashWithIndifferentAccess
result = serialize_hash(argument)
result[WITH_INDIFFERENT_ACCESS_KEY] = serialize_argument(true)
result
when Hash
symbol_keys = argument.each_key.grep(Symbol).map(&:to_s)
result = serialize_hash(argument)
result[SYMBOL_KEYS_KEY] = symbol_keys
result
else
raise SerializationError.new("Unsupported argument type: #{argument.class.name}")
end
end

Why not infer T::Boolean for true and false?

Sorbet infers the type of true to be TrueClass, and the type of false to be FalseClass. Often it would be nice if it would instead infer T::Boolean. Why not special case true and false to have the type T::Boolean instead?
It's possible to work around this problem with a type annotation, initializing variables with T.let(true, T::Boolean) for example, but it would be nice to not have to provide this extra information.
# typed: true
T.reveal_type(true) # Revealed type: `TrueClass`
T.reveal_type(false) # Revealed type: `FalseClass`
extend T::Sig
sig {params(x: T::Boolean).void}
def test(x)
var = true
10.times do
var = false # Changing the type of a variable in a loop is not permitted
end
end
The assignment of false to var in the loop causes an error to be raised, as the type of var is being changed from TrueClass to FalseClass.
Sorbet's flow-sensitive typing is made more precise by true and false having different types. In the following example, a variable with the value true is used as the condition of an if-statement:
# typed: true
val = true
if val
puts "true!"
else
puts "false?"
end
The resulting error from sorbet is:
editor.rb:7: This code is unreachable https://srb.help/7006
7 | puts "false?"
^^^^^^^^
Errors: 1
Behind the scenes, sorbet knows that the value being examined has the type TrueClass, and that the value true is the only value of that type. As a result, it knows that val cannot be false, and that the else branch will never be executed.
Now consider the case where we instead infer the type T::Boolean for true and false. T::Boolean is a synonym for T.any(TrueClass, FalseClass), so in the example it now means that val could be either true or false. As a result, it becomes impossible to tell from the type alone that the else branch will not be executed.
The flow-sensitive typing documentation on sorbet.org has more information on this topic.
Asked the same question today as well.
Ended up fixing it as you suggested with type annotations and initializing with T.let(true, T::Boolean)
# typed: true
extend T::Sig
sig {params(x: T::Boolean).void}
def test(x)
var = T.let(true, T::Boolean)
10.times do
var = T.let(false, T::Boolean)
end
end

Getting an invalid token on an interpolated string sent from python/jinga2 backend

I'm sending a variable called apiID from a tornado/jinja2 python file to my vuejs template like this:
class SmartAPIUIHandler(BaseHandler):
def get(self, yourApiID):
doc_file = "smartapi-ui.html"
dashboard_template = templateEnv.get_template(doc_file)
dashboard_output = dashboard_template.render(apiID = yourApiID )
self.write(dashboard_output)
then in vuejs I'm interpolating the variable with no problem except it gives me an error
it says: Uncaught SyntaxError: Invalid or unexpected token
I checked on the python handler file and apipID is a string, so I don't see the problem. I'm quite new to python so maybe the answer is more obvious to one of you. I appreciate the help!!
Because of dashboard_output = dashboard_template.render(apiID = yourApiID ), you must have, in your template, something around the code:
this.apiID = {{ apiID }};
Due to the value being not a number but a string, add the 's:
this.apiID = '{{ apiID }}';

grails internationalization (i18n)

i work on grails project
def result = "customer"
//(this value is according to returned method parameter,
//it may be customer, company,... & so on)
def messages = "${message(code: 'default.result.${result}', default:'${result}')}"
i need to send a variable inside message code as i mention above
problem: this code appears as
default.result.${result}
that there is no code in message.properties refer to these code
there is default.result.customer ....$ so on
Question: how can i send variable inside message Code?
Try omitting the double quotes (GString) and it should work like the following:
def xxx = "bar"
def m = message(code: "foo.${xxx}", args: ['hello world'])
Results in following message-code
foo.bar
Try:
def messages = message(code: 'default.result.' + result, default: result)
If you want to pass in some values, e.g. a string, you can define your message like this:
default.result.success = Action {0} was successfull.
And resolve your code like this:
def m = message(code: 'default.result.' + result, args: ['delete User'])