python / python/mypy

Allow __new__ to return type(None)

Aperta
#10,394 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

__new__ should be allowed to return type(None)
I know that the usual way to do this is to define a parameter as:
param: typing.Optional[str]
Sometimes developers want to customize __new__ and returning a None instance is possible and should be allowed
Adding functionality like this might be desired when they want a class to be able to do run time type checking that either str or None was ingested in new and returning the correct instance.

Here are real life samples where people using this pattern of returning None inside of __new__:

  1. https://github.com/igor-rodrigues-ss/vectorio/blob/5c77ba15eea48d2de6c5cf3000148eb149e4e790/build/lib/vectorio/vector/output/geojson/geometry.py#L10
  2. https://github.com/jpolitz/lambda-py-paper/blob/746ef63fc1123714b4adaf78119028afbea7bd76/base/pylib/none.py#L3
  3. https://www.code-learner.com/how-to-use-python-__new__-method-example/
  4. https://github.com/MrainFly/WarIV/blob/2bd7e674f715c0784fdc86358a69edea1fc3940c/Warrior/warrior.py#L67
  5. https://github.com/ClearAerospace/faa-aircraft-registry/blob/749983ca9888c3c33cc1a2522c06b186f8bc2578/faa_aircraft_registry/types.py#L192
  6. https://github.com/AGProjects/sylkserver/blob/315109881273057cd91180c93589b6fa613ef8ff/sylk/configuration/datatypes.py#L68
  7. https://github.com/romaroman/visulast/blob/8e9e89db90f8c1ecd092bef2649411b275e5e143/visulast/core/models.py#L24
  8. https://github.com/saghul/TunnelIt/blob/a1fe1c54b02dd5dc3b237f4e668952e117e769bf/tunnelit/datatypes.py#L15
  9. https://github.com/brettchien/PyBLEWrapper/blob/9ea894be978c7aab8dbd46c92e80b2bd6cfc24c2/pyble/__init__.py#L37
  10. https://github.com/maddox/vlc/blob/0dedb23704b49806c2ae4a50a72fed2fbfada4c2/bindings/python-ctypes/override.py#L168
  11. https://github.com/openstate/open-raadsinformatie/blob/10895c8e23c3e41a39886d5c61e62c8099bbb49d/ocd_backend/models/misc.py#L37

When mypy analyses the below code, an error is thrown when it should not be

To Reproduce

Run mypy on this file:

import typing

NoneType = type(None)

class NullableString:
    @typing.overload
    def __new__(cls: type, arg: None) -> 'NoneType':
        pass
    @typing.overload
    def __new__(cls: type, arg: str) -> 'NullableString':
        pass
    def __new__(cls: type, arg: typing.Optional[str]):
        if arg is None:
            return NoneType.__new__(type(None))
        elif isinstance(arg, str):
            return str.__new__(cls)

NullableString(None)

Expected Behavior

I expected mypy to not throw errors

Actual Behavior

mypy reports:

(venv) Justins-MacBook-Air:scratch justinblack$ mypy typhint/primitive.py 
typhint/primitive.py:7: error: "__new__" must return a class instance (got "None")
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used:mypy==0.812, mypy-extensions==0.4.3
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: Python 3.8.6 (default, Nov 20 2020, 18:02:11) [Clang 11.0.0 (clang-1100.0.33.17)] on darwin
  • Operating system and version: MacOS 10.14.6 (18G8022)

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo la diagnostica segnalata con lo snippet NullableString fornito e il comando mypy indicato. Traccia quindi il modo in cui mypy convalida i tipi restituiti da new, poi aggiungi una copertura di regressione per la restituzione di type(None); il lavoro è completo quando l’esempio supera il controllo dei tipi senza errori e i normali controlli di new rimangono corretti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.