python / python/mypy

Type of default value for `attrs.field()` is revealed as `Union[builtins.int, None]`

Abierto
#16,174 4 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug topic-attrs
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Bug Report

Default (default=) value for attrs.field() type is revealed as Union[builtins.int, None] where the Union with None is unexpected. While this used to be revealed as Any, this shows up as a regression, bisected to https://github.com/python/mypy/commit/391ed853f (PR https://github.com/python/mypy/pull/15021, mypy master, unreleased).

To Reproduce

import attrs

@attrs.define()
class MyDataClass:
    some_int_field: int = attrs.field(default=123)

# Somewhere else in the project I would like to reference that default:
reveal = attrs.fields(MyDataClass).some_int_field.default
reveal_type(reveal)

assignment_to_int_fails: int = attrs.fields(MyDataClass).some_int_field.default

Expected Behavior

$ mypy --strict myfile.py
myfile.py:9:13: note: Revealed type is "builtins.int"
Success: no issues found in 1 source file

Actual Behavior

on master (tested 9edda9a79790d8f7263234eca9509657ea0c37f0):

$ mypy --strict --show-error-context --show-column-numbers myfile.py
myfile.py:9:13: note: Revealed type is "Union[builtins.int, None]"
myfile.py:11:32: error: Incompatible types in assignment (expression has type "int | None", variable has type "int")  [assignment]
Found 1 error in 1 file (checked 1 source file)

(my project fails, suspected mypy bug / imperfect type inference)

On 1.5.1:

$ mypy --strict myfile.py
myfile.py:9:13: note: Revealed type is "Any"
Success: no issues found in 1 source file

(my project passes, albeit without a proper type check)

Your Environment

  • Mypy version used: master (1.5.1 = OK, introduced with https://github.com/python/mypy/commit/391ed853f)
  • Mypy command-line flags: --strict --show-error-context --show-column-numbers
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.11.5

Project to test on if you want (100% mypy-strict compatible on 1.5.1 and master otherwise): https://github.com/gertvdijk/PyKMP

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con el ejemplo mínimo de attrs del issue y ejecútalo con mypy master usando --strict, comparando el comportamiento con la versión 1.5.1. Lee el commit identificado mediante bisect, 391ed853f, y el PR #15021 para rastrear la regresión. Se considera terminado cuando el valor predeterminado se revela como int y la asignación pasa sin errores.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
compilers, devtools
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
38/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.