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.

[bug] Integer-like strings in validator defaults are incorrectly cast to integers

See original GitHub issue

When specifying a default value for a validator of "+172800", dynaconf is casting the post-validation value to an integer 172800. This is still true after explicitly adding is_type_of=str to the validator. Interestingly enough, performing another validation after this point fails the validation.

Validator section including the offending validator Screenshot from 2021-05-11 10-53-25

Example of the problem Screenshot from 2021-05-11 10-49-20

Dynaconf shouldn’t be explicitly casting default values to another type when not explicitly conflicting with the is_type_of value.

Environment:

  • OS: Fedora: 33
  • Dynaconf Version: 3.1.4
  • Frameworks in use: None

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
rochacbrunocommented, Oct 7, 2021

@JacobCallahan actually TOML takes more integer prefixes in to account https://github.com/toml-lang/toml/blob/master/toml.abnf#L116-L129

We can resolve the case for + and - as suggested.

But in any case, @andressadotpy we need to update the docs adding some notes like

# toml conversions

All values in dynaconf are parsed using toml format, TOML tries to be smart
and infer the type of the settings variables, some variables will be automatically
converted to integer:


    FOO = "0x..."  # hexadecimal
    FOO = "0o..."  # Octal
    FOO = "0b..."  # Binary

All the cases are on toml specs https://github.com/toml-lang/toml/blob/master/toml.abnf

If you need to force a specific type casting there are 2 options.

1. Use double quoted for strings ex: `FOO = "'0x...'"  will be string.
2. Specify the type using `@`  ex: FOO = "@str 0x..."
   (available converters are `@int, @float, @bool, @json`)

1reaction
JacobCallahancommented, Oct 4, 2021

@andressadotpy thanks for taking an interest in this issue! So the problem isn’t with the validator object itself, but when the default value is cast into a settings object.

In [1]: import dynaconf

In [2]: validator1 = dynaconf.validator.Validator("Testing", is_type_of=str, default="+172800")

In [3]: validator1.default
Out[3]: '+172800'

In [4]: settings = dynaconf.Dynaconf(validators=[validator1])

In [5]: settings.testing
Out[5]: 172800

In [6]: type(settings.testing)
Out[6]: int
Read more comments on GitHub >

github_iconTop Results From Across the Web

[bug] Integer-like strings in validator defaults are incorrectly ...
When specifying a default value for a validator of "+172800", dynaconf is casting the post-validation value to an integer 172800.
Read more >
Integer and string cannot be distinguished with pydantic
strings are accepted as-is, int float and Decimal are coerced using ... str are converted using v.value, and all other types cause an...
Read more >
Colander Basics - The Pylons Project
Let's define a schema which will attempt to convert a serialization to a data structure ... Int default = 10 title = 'Ranged...
Read more >
Strings - Manual - PHP
A string is series of characters, where a character is the same as a byte. ... String offsets have to either be integers...
Read more >
Custom pluggable types for ... - The Checker Framework Manual
-AcheckCastElementType In a cast, require that parameterized type arguments and array elements are the same. By default, the Checker ...
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