1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use std::collections::HashSet;

use python_parser::ast::{Expression, SetItem};

use crate::check::context::field::generic::{GenericField, GenericFields};
use crate::check::name::Name;
use crate::common::position::Position;

impl From<(&Vec<Expression>, &Option<Expression>)> for GenericFields {
    fn from((ids, ty): (&Vec<Expression>, &Option<Expression>)) -> GenericFields {
        let fields = GenericFields {
            fields: ids.iter().flat_map(|id| GenericFields::from(id).fields).collect(),
        };

        if let Some(ty) = ty {
            let name = Name::from(ty);
            if let Some(field) = fields.fields.iter().next() {
                let field = field.with_ty(&name); // cannot annotate tuples in python
                GenericFields { fields: HashSet::from([field]) }
            } else {
                fields
            }
        } else {
            fields
        }
    }
}

impl From<(&Expression, &Option<Expression>)> for GenericFields {
    fn from((id, _): (&Expression, &Option<Expression>)) -> GenericFields {
        GenericFields::from(id)
    }
}

impl From<&Expression> for GenericFields {
    fn from(id: &Expression) -> GenericFields {
        GenericFields {
            fields: (match id {
                Expression::Name(name) => vec![GenericField {
                    is_py_type: true,
                    name: name.clone(),
                    pos: Position::invisible(),
                    mutable: true,
                    in_class: None,
                    ty: None,
                    assigned_to: false, // unknown
                }],
                Expression::TupleLiteral(items) => items
                    .iter()
                    .filter(|item| matches!(item, SetItem::Unique(_)))
                    .filter(|item| match &item {
                        SetItem::Star(_) => false,
                        SetItem::Unique(expr) => matches!(expr, Expression::Name(_)),
                    })
                    .map(|item| match &item {
                        SetItem::Star(_) => unreachable!(),
                        SetItem::Unique(expression) => match expression {
                            Expression::Name(name) => GenericField {
                                is_py_type: true,
                                name: name.clone(),
                                pos: Position::invisible(),
                                mutable: false,
                                in_class: None,
                                ty: None,
                                assigned_to: false, // unknown
                            },
                            _ => unreachable!(),
                        },
                    })
                    .collect(),
                _ => vec![],
            })
                .iter()
                .cloned()
                .collect::<HashSet<_>>(),
        }
    }
}