Repository: astral-sh/ruff
File: crates/ty_python_semantic/src/place.rs (Rust)

Code before:
        use super::*;
        use crate::db::tests::setup_db;

        #[test]
        fn module_type_symbols_includes_declared_types_but_not_referenced_types() {
            let db = setup_db();
            let symbol_names = module_type_symbols(&db);

            let dunder_name_symbol_name = ast::name::Name::new_static("__name__");
            assert!(symbol_names.contains(&dunder_name_symbol_name));

            let property_symbol_name = ast::name::Name::new_static("property");
            assert!(!symbol_names.contains(&property_symbol_name));
        }
    }
}

/// Looks up the type of an "implicit class body symbol". Returns [`Place::Undefined`] if
/// `name` is not present as an implicit symbol in class bodies.
///
/// Implicit class body symbols are symbols such as `__qualname__`, `__module__`, and
/// `__firstlineno__` that Python implicitly makes available inside a class body during
/// class creation.
///
/// See <https://docs.python.org/3/reference/datamodel.html#creating-the-class-object>
pub(crate) fn class_body_implicit_symbol<'db>(
    db: &'db dyn Db,
    name: &str,
) -> PlaceAndQualifiers<'db> {
    match name {
        "__qualname__" => Place::bound(KnownClass::Str.to_instance(db)).into(),
        "__module__" => Place::bound(KnownClass::Str.to_instance(db)).into(),
        "__firstlineno__" if Program::get(db).python_version(db) >= PythonVersion::PY313 => {
            Place::bound(KnownClass::Int.to_instance(db)).into()
        }
        _ => Place::Undefined.into(),
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) enum RequiresExplicitReExport {
    Yes,
    No,
}

impl RequiresExplicitReExport {
    const fn is_yes(self) -> bool {
        matches!(self, RequiresExplicitReExport::Yes)
    }
}


Review comment (suggestion):
we also have `__doc__`, which is `str | None` (`None` if there's no class docstring):

```pycon
>>> class X:
...     """xy stuff"""
...     print(__doc__)
...     
xy stuff
```

Code after:
        use super::*;
        use crate::db::tests::setup_db;

        #[test]
        fn module_type_symbols_includes_declared_types_but_not_referenced_types() {
            let db = setup_db();
            let symbol_names = module_type_symbols(&db);

            let dunder_name_symbol_name = ast::name::Name::new_static("__name__");
            assert!(symbol_names.contains(&dunder_name_symbol_name));

            let property_symbol_name = ast::name::Name::new_static("property");
            assert!(!symbol_names.contains(&property_symbol_name));
        }
    }
}

/// Looks up the type of an "implicit class body symbol". Returns [`Place::Undefined`] if
/// `name` is not present as an implicit symbol in class bodies.
///
/// Implicit class body symbols are symbols such as `__qualname__`, `__module__`, `__doc__`,
/// and `__firstlineno__` that Python implicitly makes available inside a class body during
/// class creation.
///
/// See <https://docs.python.org/3/reference/datamodel.html#creating-the-class-object>
pub(crate) fn class_body_implicit_symbol<'db>(
    db: &'db dyn Db,
    name: &str,
) -> PlaceAndQualifiers<'db> {
    match name {
        "__qualname__" => Place::bound(KnownClass::Str.to_instance(db)).into(),
        "__module__" => Place::bound(KnownClass::Str.to_instance(db)).into(),
        // __doc__ is `str` if there's a docstring, `None` if there isn't
        "__doc__" => Place::bound(UnionType::from_elements(
            db,
            [KnownClass::Str.to_instance(db), Type::none(db)],
        ))
        .into(),
        // __firstlineno__ was added in Python 3.13
        "__firstlineno__" if Program::get(db).python_version(db) >= PythonVersion::PY313 => {
            Place::bound(KnownClass::Int.to_instance(db)).into()
        }
        _ => Place::Undefined.into(),
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) enum RequiresExplicitReExport {
    Yes,
    No,
}