django-environ icon indicating copy to clipboard operation
django-environ copied to clipboard

Default arguments should be allowed to be other than NoValue

Open fsargent opened this issue 2 years ago • 7 comments

Argument of type "Literal['']" cannot be assigned to parameter "default" of type "NoValue" in function "str" "Literal['']" is incompatible with "NoValue"PylancereportGeneralTypeIssues

CleanShot 2023-03-16 at 14 04 05@2x

I believe this would be fixed by changing the methods definitions to Union types. def str(self, var, default: Union(NoValue, str) = NOTSET, multiline=False):

fsargent avatar Mar 16 '23 21:03 fsargent

Hi @fsargent,

Thank you for bringing up this issue. I agree that the current type restriction for the default parameter can be limiting. To better understand the scope of this problem and to consider it for a future update, it would be really helpful if you could provide a test case that highlights the current limitations and your needs.

Your contribution in the form of a test would be invaluable for us to make the necessary adjustments.

sergeyklay avatar Sep 09 '23 13:09 sergeyklay

I don't quite follow what is ment by "provide a test case", but here is the use case. pyright 1.1.327 is producing error Argument of type "Literal[False]" cannot be assigned to parameter "default" of type "NoValue" in function "bool" for:

ENABLE_REDIRECTS = env.bool('ENABLE_REDIRECTS', default=False)

The type of parameter default is inferred from its default value which is NoValue.

karlisf avatar Sep 16 '23 06:09 karlisf

For example, change the implementation of env.bool() to the following removes the error:

def bool(self, var, default: bool = NOTSET):
        """
        :rtype: bool
        """
        return self.get_value(var, cast=bool, default=default)

i.e. specifying the allowable type for the default parameter

djw27 avatar Oct 17 '23 15:10 djw27

Poke! ;)

image

dokterbob avatar Jan 31 '24 16:01 dokterbob

is this project dead?

airtonix avatar May 28 '24 06:05 airtonix