Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overload type returns for nameof #77

Closed
lucasteles opened this issue Jun 16, 2022 · 6 comments
Closed

overload type returns for nameof #77

lucasteles opened this issue Jun 16, 2022 · 6 comments
Labels
proposal Proposals

Comments

@lucasteles
Copy link

the nameof always return an union, it makes it hard to use in places where we just want a string

    foo = "..."
    x1: str = nameof(foo) #Expression of type "str | Tuple[str, ...]" cannot be assigned to declared type "Tuple[str, ...]"
    x2: Tuple[str, ...] = nameof(foo) # Expression of type "str | Tuple[str, ...]" cannot be assigned to declared type "Tuple[str, ...]"
    x3: Tuple[str, ...] | str = nameof(foo) # ok

would be great to have an overload to only return Tuple when more names are passed,

or maybe a new deterministic one var function?

@pwwang
Copy link
Owner

pwwang commented Jun 16, 2022

Are you using python 3.11?

All the cases worked with python3.10:

bpython version 0.22.1 on top of Python 3.10.0 /path/to/python
>>> from varname import nameof
>>> from typing import Tuple
>>> foo = "..."
>>> x1: str = nameof(foo)
>>> x2: Tuple[str, ...] = nameof(foo)
>>> x3: Tuple[str, ...] | str = nameof(foo)
>>> x1
'foo'
>>> x2
'foo'
>>> x3
'foo'

Python 3.11 is not fully supported yet.
See also: alexmojaki/executing#31

@pwwang pwwang added the needs-more-info Not enough information to dig into the issue label Jun 16, 2022
@lucasteles
Copy link
Author

@pwwang yes I am using the 3.10

It work, but the pylance/pyright type check gives the error

@pwwang
Copy link
Owner

pwwang commented Jun 16, 2022

@overload requires the function to have the same definition. For the case of nameof, we'd need to call nameof([var1, var2]) for multiple names, which is not what I wanted it to be.

Maybe we can consider separating it into nameof (for single var) and nameof_multi (for multiple vars), but adding one more function adds complexity to the API. This issue exists with other varname core functions as well.

You can temporarily disable the type check on that line with # pyright: reportGeneralTypeIssues=false if you want for now.

@pwwang pwwang added proposal Proposals and removed needs-more-info Not enough information to dig into the issue labels Jul 21, 2022
@pwwang
Copy link
Owner

pwwang commented Aug 31, 2022

Probably https://github.com/wesselb/plum is a good idea?

@pwwang
Copy link
Owner

pwwang commented Sep 19, 2022

After some experiments, the following works:

@overload
def nameof(
    var: Any,
    more_var: Any,
    /,    # <-------
    *more_vars: Any,
) -> Tuple[str, ...]:
    ...


@overload
def nameof(
    var: Any,
) -> str:
    ...


def nameof(
    var: Any,
    *more_vars: Any,
) -> str | Tuple[str, ...]:
    ...

However, / was introduced in python3.8. Without it, mypy reports an error.

@pwwang
Copy link
Owner

pwwang commented Dec 6, 2022

Link commit 1a4c450

@pwwang pwwang mentioned this issue Feb 16, 2023
@pwwang pwwang closed this as completed Apr 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal Proposals
Projects
None yet
Development

No branches or pull requests

2 participants