MCPcopy Create free account
hub / github.com/astral-sh/ruff / build_predicate

Method build_predicate

crates/ty_python_core/src/builder.rs:1848–1883  ·  view source on GitHub ↗
(&mut self, predicate_node: &'ast ast::Expr)

Source from the content-addressed store, hash-verified

1846 }
1847
1848 fn build_predicate(&mut self, predicate_node: &'ast ast::Expr) -> PredicateOrLiteral<'db> {
1849 // Some commonly used test expressions are eagerly evaluated as `true`
1850 // or `false` here for performance reasons. This list does not need to
1851 // be exhaustive. More complex expressions will still evaluate to the
1852 // correct value during type-checking.
1853 fn resolve_to_literal(node: &ast::Expr) -> Option<bool> {
1854 match node {
1855 ast::Expr::BooleanLiteral(ast::ExprBooleanLiteral { value, .. }) => Some(*value),
1856 node if is_if_type_checking(node) => Some(true),
1857 ast::Expr::NumberLiteral(ast::ExprNumberLiteral {
1858 value: ast::Number::Int(n),
1859 ..
1860 }) => Some(*n != 0),
1861 ast::Expr::EllipsisLiteral(_) => Some(true),
1862 ast::Expr::NoneLiteral(_) => Some(false),
1863 ast::Expr::UnaryOp(ast::ExprUnaryOp {
1864 op: ast::UnaryOp::Not,
1865 operand,
1866 ..
1867 }) => Some(!resolve_to_literal(operand)?),
1868 _ => None,
1869 }
1870 }
1871
1872 self.register_narrowing_alias_predicates(predicate_node);
1873
1874 let expression = self.add_standalone_expression(predicate_node);
1875
1876 match resolve_to_literal(predicate_node) {
1877 Some(literal) => PredicateOrLiteral::Literal(literal),
1878 None => PredicateOrLiteral::Predicate(Predicate {
1879 node: PredicateNode::Expression(expression),
1880 is_positive: true,
1881 }),
1882 }
1883 }
1884
1885 /// Adds a new predicate to the list of all predicates, but does not record it. Returns the
1886 /// predicate ID for later recording using

Callers 3

visit_stmt_implMethod · 0.80
visit_exprMethod · 0.80

Calls 5

LiteralEnum · 0.85
PredicateClass · 0.85
ExpressionClass · 0.85

Tested by

no test coverage detected