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

[Python 3.9] Value 'Optional' is unsubscriptable (unsubscriptable-object) (also Union) #3882

Closed
hjwp opened this issue Oct 7, 2020 · 25 comments · Fixed by #3890
Closed

[Python 3.9] Value 'Optional' is unsubscriptable (unsubscriptable-object) (also Union) #3882

hjwp opened this issue Oct 7, 2020 · 25 comments · Fixed by #3890
Labels
Blocker 🙅 Blocks the next release Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code python 3.9

Comments

@hjwp
Copy link

hjwp commented Oct 7, 2020

Steps to reproduce

Install Python 3.9, then:

# minimal_repro.py
from typing import Optional, Union
foo: Optional[int] = 3
bar: Union[int, str] = 4

Current behavior

Pylint reports

minimal_repro.py:3:5: E1136: Value 'Optional' is unsubscriptable (unsubscriptable-object)
minimal_repro.py:4:5: E1136: Value 'Union' is unsubscriptable (unsubscriptable-object)

Expected behavior

This isn't an error in Python 3.8 so I don't think it should be an error in 3.9?

pylint --version output

pylint 2.6.0
astroid 2.4.2

(I tried pip install pylint astroid --pre -U)

@dkmiller
Copy link

dkmiller commented Oct 7, 2020

Also encountering this with the Union type in Python 3.9: https://github.com/dkmiller/pyconfigurableml/pull/16/checks?check_run_id=1217453549

@hjwp hjwp changed the title [Python 3.9] Value 'Optional' is unsubscriptable (unsubscriptable-object) [Python 3.9] Value 'Optional' is unsubscriptable (unsubscriptable-object) (also Union) Oct 7, 2020
@hjwp
Copy link
Author

hjwp commented Oct 7, 2020

have updated the issue to cover Union as well.

interestingly, it doesn't seem to be a problem for some other types that use the subscript syntax, eg List:

baz: List[int] = [1, 2, 3]   # this is fine.

@ReagentX
Copy link

ReagentX commented Oct 8, 2020

This is definitely a false positive, the docs haven't changed: https://docs.python.org/3/library/typing.html#typing.Optional

@JulienPalard
Copy link
Contributor

JulienPalard commented Oct 9, 2020

Self-contained reproducer, which is not Python 3.9 dependent (but the typing issue is 3.9 dependent):

class deco:
    def __init__(self, f):
        self.f = f

    def __getitem__(self, item):
        return item


@deco
def Optional():
    pass


print(Optional[int])

Probably related to #2578:

pylint doesn't currently correctly understand decorators

Looks like some code give up checks if the function is decorated, IIRC:

if isinstance(owner, astroid.FunctionDef) and owner.decorators:
    return False

it would be a simple quick-fix if we have no good way of studying the decorator from now.

As my reproducer is not 3.9 specific I'm digging about why the initial reproducer is failing on 3.9:

  • The inferred function on line 1722 of typecheck.py is Optional in 3.9
  • The inferred function on line 1722 of typecheck.py is Union in 3.8

Optional in 3.8 is declared as:

Optional = _SpecialForm('Optional', doc=...)

while in 3.9 it's declared as:

@_SpecialForm
def Optional(self, parameters):
    ...

As pylint does not resolve decorator, the inference is still OK in 3.8 (no decorator) but fails in 3.9 (decorated).

Looks like it could be fixed by using something like:

if inferred.decorators:
    first_decorator = helpers.safe_infer(inferred.decorators.nodes[0])
    if isinstance(first_decorator, astroid.ClassDef):
        inferred = first_decorator.instantiate_class()
    else:
        return  # TODO: Handle decorators that are not classes

in visit_subscript in typecheck.py.

rjarry added a commit to sysrepo/sysrepo-python that referenced this issue Oct 16, 2020
Pylint does not support python 3.9 properly:

  Value 'Optional' is unsubscriptable (unsubscriptable-object)

This is a known issue. Until it is fixed run the lint checks with python
3.8.

Link: pylint-dev/pylint#3882
Signed-off-by: Robin Jarry <robin.jarry@6wind.com>
@hippo91
Copy link
Contributor

hippo91 commented Oct 17, 2020

Thanks to all for your reports. I'm not sure the issue illustrated by @JulienPalard is the same as the original one.
We have to work on supporting python 3.9 but for now it is not yet fully supported.

@hippo91 hippo91 added Blocker 🙅 Blocks the next release Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code python 3.9 labels Oct 17, 2020
kmyk added a commit to online-judge-tools/oj that referenced this issue Oct 22, 2020
rjarry added a commit to CESNET/libyang-python that referenced this issue Oct 29, 2020
Pylint does not support python 3.9 properly:

  Value 'Optional' is unsubscriptable (unsubscriptable-object)

This is a known issue. Until it is fixed run the lint checks with python
3.8.

Link: pylint-dev/pylint#3882
Signed-off-by: Robin Jarry <robin.jarry@6wind.com>
XanClic pushed a commit to XanClic/qemu that referenced this issue Nov 3, 2020
When run with Python 3.9, pylint incorrectly warns about things like
Optional[foo] because it doesn't recognise Optional as unsubscriptable.
This is a known pylint bug:

    pylint-dev/pylint#3882

Just disable this check to get rid of the warnings.

Disabling this shouldn't make us miss any real bug because mypy also
has a similar check ("... is not indexable").

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
qemu-deploy pushed a commit to qemu/qemu that referenced this issue Nov 3, 2020
When run with Python 3.9, pylint incorrectly warns about things like
Optional[foo] because it doesn't recognise Optional as unsubscriptable.
This is a known pylint bug:

    pylint-dev/pylint#3882

Just disable this check to get rid of the warnings.

Disabling this shouldn't make us miss any real bug because mypy also
has a similar check ("... is not indexable").

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Message-Id: <20201027163806.290960-3-kwolf@redhat.com>
Reviewed-by: John Snow <jsnow@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
TeoZosa added a commit to TeoZosa/structlog-sentry-logger that referenced this issue Nov 4, 2020
Manually disabling spurious error message arising from a bug in pylint
for Python 3.9, i.e.,

```
pylint...............................................................................Failed
- hook id: pylint
- exit code: 2

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

************* Module structlog_sentry_logger._config
structlog_sentry_logger/_config.py:240:18: E1136: Value 'Union' is unsubscriptable (unsubscriptable-object)

------------------------------------------------------------------
Your code has been rated at 9.52/10 (previous run: 9.52/10, +0.00)

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)
```
see: [[Python 3.9] Value 'Optional' is unsubscriptable
(unsubscriptable-object) (also Union) #3882](pylint-dev/pylint#3882)
lukany added a commit to lukany/ggci that referenced this issue Jun 14, 2021
This fixes false positive errors for Python 3.9.
See pylint-dev/pylint#3882
aaditya-chandrasekhar pushed a commit to val-verde/graphviz that referenced this issue Aug 18, 2021
This avoids a Pylint bug¹ that prevents adding a --errors-only check that would
fail the build on failure.

¹ pylint-dev/pylint#3882
simmel added a commit to simmel/pinboard-queue that referenced this issue Sep 19, 2021
paride added a commit to paride/pycloudlib that referenced this issue Dec 13, 2021
This solves a E1136 false positive with Python 3.9 [1], but the code
needs some changes to test clean.

[1] pylint-dev/pylint#3882
paride added a commit to canonical/pycloudlib that referenced this issue Dec 13, 2021
This solves a E1136 false positive with Python 3.9 [1], but the code
needs some changes to test clean.

[1] pylint-dev/pylint#3882
Athesdrake added a commit to Athesdrake/aiotfm that referenced this issue Dec 28, 2021
hswong3i pushed a commit to alvistack/python-debian-team-python-debian that referenced this issue Mar 12, 2022
Until pylint is fixed, unsubscriptable-object generates lots of false
positives. This commit can be reverted with pylint > 2.6.0.

See
 pylint-dev/pylint#3882
cpatrasciuc added a commit to cpatrasciuc/schnapsen-card-game that referenced this issue Jun 17, 2022
…tives caused by Optional fields in PlayerPair initialized to None. See pylint-dev/pylint#3882.
mristin pushed a commit to Parquery/pylddwrap that referenced this issue Oct 13, 2022
mbolivar-nordic added a commit to mbolivar-nordic/sdk-nrf that referenced this issue Mar 7, 2023
The current version of pylint is 2 years old at this point,
and it's got a bug (pylint-dev/pylint#3882)
which is leading to false positives on ncs_west_helpers.py:

 E1136:Value 'Union' is unsubscriptable (unsubscriptable-object)
File:scripts/west_commands/ncs_west_helpers.py
Line:78
Column:36
 E1136:Value 'Union' is unsubscriptable (unsubscriptable-object)
File:scripts/west_commands/ncs_west_helpers.py
Line:326
Column:18
 E1136:Value 'Union' is unsubscriptable (unsubscriptable-object)
File:scripts/west_commands/ncs_west_helpers.py
Line:78
Column:36
 E1136:Value 'Union' is unsubscriptable (unsubscriptable-object)
File:scripts/west_commands/ncs_west_helpers.py
Line:326
Column:18
 E1136:Value 'Optional' is unsubscriptable (unsubscriptable-object)
File:scripts/west_commands/pygit2_helpers.py
Line:188
Column:46
 E1136:Value 'Optional' is unsubscriptable (unsubscriptable-object)
File:scripts/west_commands/ncs_commands.py
Line:44
Column:38

Update it to the latest version. We have to update astroid,
typing-extensions, and colorama as well as a side effect.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
mbolivar-nordic added a commit to nrfconnect/sdk-nrf that referenced this issue Mar 7, 2023
The current version of pylint is 2 years old at this point,
and it's got a bug (pylint-dev/pylint#3882)
which is leading to false positives on ncs_west_helpers.py:

 E1136:Value 'Union' is unsubscriptable (unsubscriptable-object)
File:scripts/west_commands/ncs_west_helpers.py
Line:78
Column:36
 E1136:Value 'Union' is unsubscriptable (unsubscriptable-object)
File:scripts/west_commands/ncs_west_helpers.py
Line:326
Column:18
 E1136:Value 'Union' is unsubscriptable (unsubscriptable-object)
File:scripts/west_commands/ncs_west_helpers.py
Line:78
Column:36
 E1136:Value 'Union' is unsubscriptable (unsubscriptable-object)
File:scripts/west_commands/ncs_west_helpers.py
Line:326
Column:18
 E1136:Value 'Optional' is unsubscriptable (unsubscriptable-object)
File:scripts/west_commands/pygit2_helpers.py
Line:188
Column:46
 E1136:Value 'Optional' is unsubscriptable (unsubscriptable-object)
File:scripts/west_commands/ncs_commands.py
Line:44
Column:38

Update it to the latest version. We have to update astroid,
typing-extensions, and colorama as well as a side effect.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
wannacfuture added a commit to wannacfuture/Tuto_Django that referenced this issue Jun 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blocker 🙅 Blocks the next release Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code python 3.9
Projects
None yet
Development

Successfully merging a pull request may close this issue.